![]() ![]() ![]() ![]() ![]() ![]() |
Windows Forms supports ErrorProvider help on controls for data input validation. Normally validation doesn't go much further than a check of the maximum length for textboxes, But this is a nice method you can use to make validation a breeze.

When a validation error appears the red error icon occurs and one can hover it for a tooltip with information about the invalid field. The error icon method is much better than a message box pop-up, because that would be an unnecessary interruption. So we get a method of unobtrusively showing that data is invalid, and presenting it in such a way that it is immediately obvious what is wrong and what to do about it.
To provide ErrorProvider help in your application, you have to use the ErrorProvider component which you can find in the Toolbox on the Windows Forms tab. Drag and Drop a ErrorProvider component from the Toolbox to your form. It is a component rather than a control, so when you put it on your form it will sit in the component tray below. Rename this component to "erpProvider1". This component gives every control on the form a new property called Error on erpProvider1.

You can set the Error on erpProvider1 property of the ZipCode field to "Please enter 5 digits" but this isn't really the intent of the ErrorProvider component. You see the red icon and the same effect could be achieved with a ToolTip component.

The better idea is t create an event handler for the TextBox on the form and add the following code to it. To clear the error , simply pass an empty string.
Private Sub cmdInputValidation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInputValidation.Click
Dim sAddressEmail As String = txtAddressEmail.Text
Dim sAddressZipCode As String = txtAddressZipCode.Text
Dim sAddressTownName As String = txtAddressTownName.Text
Dim sMessage As String
Dim IsValidationError As Boolean = False
'----- set error message when @ is missing -----
If InStr(sAddressEmail, "@") = 0 Then
erpProvider1.SetError(Me.txtAddressEmail, "Missing '@' - Please enter the complete e-mail address")
IsValidationError = True
Else
erpProvider1.SetError(Me.txtAddressEmail, "")
End If
'----- input validation of ZipCode -----
If Not IsNumeric(sAddressZipCode) Or Len(sAddressZipCode) <> 5 Then
erpProvider1.SetError(Me.txtAddressZipCode, "Please enter 5 digits")
IsValidationError = True
Else
erpProvider1.SetError(Me.txtAddressZipCode, "")
End If
'----- set error message when len = 0 -----
If Len(Trim(sAddressTownName)) = 0 Then
erpProvider1.SetError(Me.txtAddressTownName, "Please enter the town name")
IsValidationError = True
Else
erpProvider1.SetError(Me.txtAddressTownName, "")
End If
'--- Continue when all the text boxes are valid
If IsValidationError = True Then
sMessage = sAddressEmail & " " & sAddressZipCode & " " & sAddressTownName
MessageBox.Show(sMessage, "Error occured - Stop processing code")
Exit Sub
End If
sMessage = sAddressEmail & " " & sAddressZipCode & " " & sAddressTownName
MessageBox.Show(sMessage, "No Error occured - Continue processing code")
End Sub
Dim ctrl As Control
Dim sErrorList As Strimg
sErrorList = ""
For Each ctrl In Me.Controls
If Len(ErrorProvider1.GetError(ctrl)) > 0 Then
sErrorList += ErrorProvider1.GetError(ctrl) & ChrW(10) & ChrW(13)
End If
Next
If Len(sErrorList) = 0 Then
' Process stuff if no errors
Else
Messagebox.Show(sErrorList, "List Of Errors")
End If
You can have a second error provider on your form with a yellow icon (developer defined) rather than red, for when a field is valid but could be entered better.
Unfortunately, you don't have Visual Studio .NET but you have installed the Microsoft .NET Framework 1.0 (e.g. from your CD-Version of Windows XP SP1) unzip the example for Visual Studio 2002 and double-click the file VBnetCHM.exe.