Visual Studio 2010 ErrorProvider control in VB.NET
In this article we will lean how to use errorprovider control in windows forms application using Visual Studio 2010.
In this article we will lean how to use errorprovider control in Windows Forms application using Visual Studio 2010.
ErrorProvider control:
The errorprovider control provides a user interface to indicate to the user that a control on a form has an error associated with it, or The ErrorProvider component provides an easy way to set validation errors. It allows us to set an error message for any control on the form when the input is not valid. When an error message is set, an icon indicating the error will appear next to the control and the error message is displayed as Tool Tip when the mouse is over the control.
errorprovider properties:
Icon - Icon property are used to indicate an error.
ContainerControl - ContainerControl property are used to parent control, usually the form, that contains the data-bound controls on which the errorprovider can display error icon.
BlinkStyle - Controls whether the error icon blinks when an error is set.
Figure 1.
Working with errorprovider:
Drag a errorprovider, TextBox and a Button from the Toolbox. Form looks like this.
Figure 2.
Now double click on the form and write this code.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ErrorProvider1.SetError(TextBox1, " Please enter the text ")
End Sub
Now run the application. Textbox displays an error icon.
Figure 3.
Example:
Now work with an example. Assume we have a TextBox and a Button on a form. If the TextBox on the form is left blank and if the Button is clicked, an icon will be displayed to the TextBox and the specified text will appear in the Tool Tip box when the mouse is over the control. The code for that looks like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
ErrorProvider1.SetError(TextBox1, " Please enter the text do not leave blank")
Else
ErrorProvider1.SetError(TextBox1, "")
End If
Now run the application and click on the button without write any text in the textbox. it displays the tool tip and error icon.
Figure 4.
Now write some text in the text box and click on the button. It does not display the tool tip and error icon.
Figure 5.