How to use Error Provider in C#

By FoxLearn 2/7/2025 4:15:06 AM   19.48K
In C# Windows Forms, the ErrorProvider component is used to provide visual indications of errors or warnings associated with user input controls, such as textboxes or comboboxes.

The C# ErrorProvider allows you to display error messages to users when they enter invalid data in a form with a small icon.

What is the ErrorProvider in WinForms?

The WinForms ErrorProvider is a control in Windows Forms applications that provides a way to display error icons next to controls that contain invalid data. The control is not visible itself, but it displays a small icon (usually a red icon with a cross) next to the controls like text boxes or combo boxes where an error has occurred.

How to use Error Provider in C#?

Using the ErrorProvider in your WinForms application is straightforward.

First, Create a new Windows Forms application project, next enter your project name is "ValidateDemo" and then click OK button.

Drag and drop the TextBox, Label and Button controls from your Visual Studio toolbox to your winform, then design a simple UI login form as shown below.

error provider in c#

You need to add the ErrorProvider component to your form. You can do this by either dragging it from the Toolbox onto your form or by adding it programmatically.

ErrorProvider presents a simple mechanism for indicating to the end user that a control on a form has an error associated with it.

After adding the ErrorProvider to your form, you need to associate it with the controls you want to validate. This is typically done in response to events such as validating or validating the user input. You can do this either in the form designer or in your code.

You need to write code to validate user input. This could involve checking if the input meets certain criteria, such as being non-empty or in a specific format. If the input is invalid, you set the error message for the associated control using the ErrorProvider.

If an error description string is specified for the control, an icon appears next to the control. Next, add the click event handler to the Login button as the following c# code.

For example, how to use the c# errorprovider

private void btnLogin_Click(object sender, EventArgs e)
{
    if (ValidateChildren(ValidationConstraints.Enabled))
    {
        MessageBox.Show(txtUsername.Text, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

Add the validating event handler to the TextBox allows you to check validate the TextBox control.

private void txtUsername_Validating(object sender, CancelEventArgs e)
{
    if (string.IsNullOrEmpty(txtUsername.Text))
    {
        e.Cancel = true;
        txtUsername.Focus();
        // c# errorprovider show error
        errorProvider.SetError(txtUsername, "Please enter your user name !");
    }
    else
    {
        e.Cancel = false;
        errorProvider.SetError(txtUsername, null);
    }
}

The Validating event of txtUserName is used to check if the textbox is empty. If it's empty, an error message is displayed using the ErrorProvider.

The btnLogin_Click event is used to manually trigger the validation of all controls on the form.

If the user input is invalid, you display an error message using the ErrorProvider component. This typically involves calling the SetError method of the ErrorProvider component and passing the control and the error message.

When the user corrects the input or the error condition is resolved, you should clear the error message associated with the control. You can do this by calling the Clear method of the ErrorProvider component.

If you want to change icon errorprovider c#, you can add an imagelist control to your winform, then set icon to the error provider control as the following c# code.

this.errorProvider.Icon = Icon.FromHandle(((Bitmap)imageList.Images[0]).GetHicon());

You can also change the blink style to NeverBlink if you don't want to scare the user.

this.errorProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;

You can also create an instance of ErrorProvider in your code behind to validate user inputs and show the error message accordingly.

// Create an instance of ErrorProvider
ErrorProvider errorProvider = new ErrorProvider();

// Set the error message for a TextBox control if validation fails
if (string.IsNullOrEmpty(txtName.Text))
{
    errorProvider.SetError(txtName, "Name cannot be empty.");
}
else
{
    errorProvider.SetError(txtName, string.Empty); // Clears the error
}

You can customize how the ErrorProvider works, including setting the icon type, and positioning the error icon as shown above.

Once the error is fixed, you can clear the error message like this:

errorProvider.Clear();

Benefits of Using the ErrorProvider in C#

  • Improved UX: By showing error messages in real-time, users can correct mistakes as they happen, preventing frustration and improving user experience.
  • Simplicity: The ErrorProvider control is easy to set up and use, requiring only a few lines of code to show or clear error messages.
  • Non-Intrusive: Unlike message boxes, the ErrorProvider is non-intrusive and does not disrupt the flow of the application.

VIDEO TUTORIAL