Windows Phone Developers

Thursday, July 31, 2008

Validate Windows Form Controls in Vb.Net

Validation Controls for Windows Forms (VB.NET)

The Windows Forms ErrorProvider component is used to validate user input on a form or control. It is typically used in conjunction with validating user input on a form, or displaying errors within a dataset. An error provider is a better alternative than displaying an error message in a message box, because once a message box is dismissed, the error message is no longer visible. The ErrorProvider component displays an error icon ( ) next to the relevant control, such as a text box; when the user positions the mouse pointer over the error icon, a ToolTip appears, showing the error message string.


ErrorProvider Component

Drag the component to the Windows Forms Designer

ErrorProvider Component Attached to Designer


The ErrorProvider component's key properties are DataSource, ContainerControl, and Icon. When using ErrorProvider component with data-bound controls, the ContainerControl property must be set to the appropriate container (usually the Windows Form) in order for the component to display an error icon on the form. When the component is added in the designer, the ContainerControl property is set to the containing form; if you add the control in code, you must set it yourself.

The Icon property can be set to a custom error icon instead








The key method of the ErrorProvider component is the SetError method, which specifies the error message string and where the error icon should appear.

The code below uses ErrorProvider1.SetError instead of the usual msgbox for displaying errors.

Private Sub MaskedTextBox1_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected

If MaskedTextBox1.MaskFull Then

ErrorProvider1.SetError(MaskedTextBox1, "Please do not attempt to type extra data")

ElseIf e.Position = MaskedTextBox1.Mask.Length Then

ErrorProvider1.SetError(MaskedTextBox1, "End of Input: If necessary modify appropriate data by typeover")

Else

ErrorProvider1.SetError(MaskedTextBox1, "Only numerals are accepted")

End If

End Sub

The icon will appear next to the component that has error. This can be customized for your application. When you hover mouse over the icon, the error message will be displayed as a tooltip

Windows Forms ErrorProvider component in VB.NET, SetError Method in VB.NET, VB.NET Windows form Validation, Display error messages in tooltips using Vb.NET, VB.NET Windows Form Controls

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

No comments:

Post a Comment