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.
  • 2280
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. The ErrorProvider component displays an error icon (untitled.JPG) 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 DataSource property 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

  1. Add two controls - for example, text boxes - to a Windows Form.
  2. Add an ErrorProvider component to the form.
  3. Select the first control and add code to its Validating event handler. In order for this code to run properly, the procedure must be connected to the event. For more information, see Creating Event Handlers on the Windows Forms Designer, Creating Event Handlers at Run Time for Windows Forms, and Creating Event Handlers in the Visual Basic Code Editor.

    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.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.