C# ErrorProvider

ErrorProvider simplifies and streamlines error presentation. It is an abstraction that shows errors on your form. It does not require a lot of work on your part—this is its key feature.

ErrorProvider

This C# article demonstrates the ErrorProvider control in Windows Forms.

Get started

Steps

To start, please open the Toolbox pane in Visual Studio and double-click on the ErrorProvider icon. This will insert the errorprovider1 into the tray at the bottom of the screen. Next, you can change properties on the ErrorProvider instance by right-clicking on it and selecting Properties. Some properties and methods on the ErrorProvider are detailed in the following sections.

Example code

Let's activate the ErrorProvider in our Windows Forms program. The ErrorProvider won't initiate any actions, but will be invoked through other event handlers in the program. In the example, we have a TextBox control and a TextChanged event handler on that control. When the TextChanged event handler is triggered, we check to see if a digit character is present. If one is not, we activate an error through the ErrorProvider; otherwise, we clear the ErrorProvider.

Example that uses ErrorProvider [C#]

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void textBox1_TextChanged(object sender, EventArgs e)
	{
	    // Determine if the TextBox has a digit character.
	    string text = textBox1.Text;
	    bool hasDigit = false;
	    foreach (char letter in text)
	    {
		if (char.IsDigit(letter))
		{
		    hasDigit = true;
		    break;
		}
	    }
	    // Call SetError or Clear on the ErrorProvider.
	    if (!hasDigit)
	    {
		errorProvider1.SetError(textBox1, "Needs to contain a digit");
	    }
	    else
	    {
		errorProvider1.Clear();
	    }
	}
    }
}
Warning

Using SetError method. Please look at the arguments of the SetError method in the textBox1_TextChanged event handler. The first argument is the identifier of the TextBox control; the second argument is the error message itself. The first argument tells the ErrorProvider where to draw the error sign.

Using Clear method. It is also important that you use the Clear method on the ErrorProvider when appropriate. If a digit was located, we simply remove the error sign from the user interface; this instantly tells our user that no error is still present.

ErrorProvider and TextChanged event

Set BlinkStyle

The ErrorProvider has two properties related to blinking: the BlinkRate, which indicates how many milliseconds each blink is apart; and the BlinkStyle, which lets you disable or enable blinking in general. Typically, for less complicated user interfaces, blinking isn't necessary because the ErrorProvider will be easy to see. For more complex interfaces, the blinking could draw attention to the error.

Tip: In this example, setting BlinkStyle to NeverBlink is appropriate.

ErrorProvider versus dialogs

Programming tip

One of the most frustrating experiences for a computer user is getting dialog boxes that demand your entire attention. The ErrorProvider can alleviate this frustration. Also, the ErrorProvider can alert the user to more than one error at a time. Having to dismiss more than one dialog is extremely obnoxious to your busy users.

Summary

.NET Framework information

The ErrorProvider control in the Windows Forms framework provides a simple-to-understand abstraction for presenting errors on input. Importantly, it reduces the frustration associated with dialog window use, and can also streamline error corrections by presenting many errors at once.

Windows Forms
.NET