ErrorProvider component in VB.NET
The Windows Forms ErrorProvider component allows you to show the user in a non-intrusive way that something is wrong.
The Windows Forms ErrorProvider component allows you to show the user in a non-intrusive way that something is wrong. It is typically used in conjunction with validating user input on a form, or displaying errors within a dataset.
Introduction
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. TheErrorProvider 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.
The ErrorProvider component's key properties are DataSource, ContainerControl, and Icon. The ContainerControl property must be set to the appropriate container (usually the Windows Form) in order for the ErrorProvider 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 of the default. When the DataSourceproperty is set, the ErrorProvider component can display error messages for a dataset. The key method of the ErrorProvider component is the SetError method, which specifies the error message string and where the error icon should appear.
Displaying Error Icons for Form Validation with the Windows Forms ErrorProvider Component
You can use a Windows Forms ErrorProvider component to display an error icon when the user enters invalid data. You must have at least two controls on the form in order to tab between them and thereby invoke the validation code.
To display an error icon when a control's value is invalid
-
Add two controls - for example, text boxes - to a Windows Form.
-
Add an ErrorProvider component to the form.
-
The following code tests the validity of the data the user has entered; if the data is invalid, the SetError method is called. The first argument of the SetError method specifies which control to display the icon next to. The second argument is the error text to display.
Private Sub TextBox1_Validating(ByVal Sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles _
TextBox1.Validating
If Not IsNumeric(TextBox1.Text) Then
ErrorProvider1.SetError(TextBox1, "Not a numeric value.")
Else
' Clear the error.
ErrorProvider1.SetError(TextBox1, "")
End If
End Sub
Viewing Errors within a DataSet with the Windows Forms ErrorProvider Component
You can use the Windows Forms ErrorProvider component to view column errors within a dataset or other data source. For an ErrorProvider component to display data errors on a form, it does not have to be directly associated with a control. Once it is bound to a data source, it can display an error icon next to any control that is bound to the same data source.
Note If you change the error provider's DataSource and DataMember properties at run time, you should use the BindToDataAndErrors method to avoid conflicts. For more information, see ErrorProvider.BindToDataAndErrors Method.
To display data errors
-
Bind the component to a specific column within a data table.
-
Set the ContainerControl property to the form.
ErrorProvider1.ContainerControl = Me
-
Set the position of the current record to a row that contains a column error.
DataTable1.Rows(5).SetColumnError("Name", "Bad data in this row.")
Me.BindingContext(DataTable1).Position = 5