Error Provider is one of the many components in VB.NET that lets your application display error messages once a control triggers its validating or validated event. To understand, here's a step-by-step procedure on how to use the Error Provider:
1. Double click or drag the error provider button into the form. The error provider named ErrorProvider1 appears at the bottom of the design area as shown below:
2. Create a form exactly as shown below and change the name of TextBox1 and TextBox2 into txtTelNo1 and txtTelNo2 in the Properties Window.
3. Type the following in the validating event of txtTelNo1 and txtTelNo2:
Private Sub txtTelNo1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTelNo1.Validating
ValidateTelNo(DirectCast(sender, TextBox))
End Sub
Private Sub txtTelNo2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTelNo2.Validating
ValidateTelNo(DirectCast(sender, TextBox))
End Sub
4. Next, type the following subroutine that takes the txtTelNo1 and txtTelNo2 as its parameter and sets the error in the ErrorProvider1 for textbox that doesn't contain exactly 7 digits.
Private Sub ValidateTelNo(ByVal txtTelNo As TextBox)
If (txtTelNo.Text.Length <> 0) And Not (txtTelNo.Text Like "#######") Then
ErrorProvider1.SetError(txtTelNo, txtTelNo.Name & " must contain exactly 7 digits")
Else
ErrorProvider1.SetError(txtTelNo, "")
End If
End Sub
5. For the Button1 Click Event, type the following:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If IsNotValidTelNo(txtTelNo1) Then Exit Sub
If IsNotValidTelNo(txtTelNo2) Then Exit Sub
MessageBox.Show("Success!")
End Sub
6. Finally, type the following function used in Button1.Click event that takes a textbox as its parameter and returns True or False:
Private Function IsNotValidTelNo(ByVal txtTelNo As TextBox) As Boolean
If ErrorProvider1.GetError(txtTelNo).Length = 0 Then
Return False
Else
MessageBox.Show(ErrorProvider1.GetError(txtTelNo))
txtTelNo.Focus()
Return True
End If
End Function
The program checks for the number of digits contained in the textbox and prompts the user for any error once the button is clicked.



