In this article I will explain with an example, how to use ErrorProvider control in Windows Forms Application using C# and VB.Net.
 
 
What is ErrorProvider?
The ErrorProvider control is used to display an alert (an icon appears next to the control) to the user, when controls on the Form has error associated with it.
When the mouse hovers over the icon, a ToolTip appears showing the error message, if an error description string (message) is specified for the control.
The ErrorProvider control is present in the Components Tab of the Visual Studio ToolBox as shown below.
Using ErrorProvider control in Windows Forms with C# and VB.Net
 
The ErrorProvider has following properties.
1. Icon - Gets or sets the Icon that is displayed next to a control, when an error description string has been set for the control.
2. BlinkRate - Gets or sets the rate at which the error icon flashes.
3. HasErrors - Gets a value that indicates if this ErrorProvider has any errors for any of the associated controls.
 
The ErrorProvider has following methods.
1. SetError(Control, String) - Sets the error description string for the specified control.
2. Clear() - Clears all settings associated with this component.
Note: For more details about ErrorProvider, please refer MSDN.
 
 
Example
Form Design
The Form consists of a Label, a TextBox, an ErrorProvider control and a Button.
Using ErrorProvider control in Windows Forms with C# and VB.Net
 
 
Assigning Validating event handler to TextBox
Inside the Properties window, TextBox is assigned with Validating event handler.
Using ErrorProvider control in Windows Forms with C# and VB.Net
 
Inside the Validating event handler, the TextBox is validated for Empty (Blank) and White space and if the validation fails i.e. if the TextBox is empty (blank), an error message is set in the ErrorProvider control..
C#
privatevoid txtName_Validating(object sender, CancelEventArgs e)
{
    if (!string.IsNullOrEmpty(txtName.Text.Trim()))
    {
        epName.SetError(txtName, string.Empty);
    }
    else
    {
        epName.SetError(txtName, "Name is required.");
    }
}
 
VB.Net
Private Sub txtName_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtName.Validating
    If Not String.IsNullOrEmpty(txtName.Text.Trim) Then
        epName.SetError(txtName, String.Empty)
    Else
        epName.SetError(txtName, "Name is required.")
    End If
End Sub
 
 
Screenshot
Using ErrorProvider control in Windows Forms with C# and VB.Net
 
 
Downloads