How to use Error Provider in C#

By FoxLearn 7/19/2024 2:30:30 AM   18.61K
Using Error Provider in C# Windows Forms Application to Display Error Icons for Form validation.

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.

How to use Error Provider in C#

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.

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();
        //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;

VIDEO TUTORIAL