C# ErrorProvider Control: Windows FormsUse the ErrorProvider control in Windows Forms to display errors in user input.
dot net perls
ErrorProvider, a Windows Forms control, 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.
Example. We start with the basics. We must activate the ErrorProvider in our Windows Forms program. The ErrorProvider won't initiate any actions. It will be invoked through other event handlers in the program.
Note In the example, we have a TextBox control and a TextChanged event handler on that control.
Info When the TextChanged event handler is triggered, we check to see if a digit character is present.
And If one is not, we activate an error through the ErrorProvider. Otherwise we clear the ErrorProvider.
Start. To start, please open the Toolbox pane in Visual Studio. Find 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.
Note Some properties and methods on the ErrorProvider are detailed in the following sections.
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(); } } } }
Using SetError method. The first argument of SetError 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 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.
BlinkStyle. The ErrorProvider has two properties related to blinking. The BlinkRate indicates how many milliseconds each blink is apart. And the BlinkStyle lets you disable or enable blinking in general.
Note Typically, for less complicated user interfaces, blinking isn't necessary because the ErrorProvider will be easy to see.
Tip For more complex interfaces, the blinking could draw attention to the error. Setting BlinkStyle to NeverBlink is appropriate.
Dialogs. A frustrating experience for a computer user is getting dialog boxes that demand your entire attention. The ErrorProvider alleviates this frustration. Also, the ErrorProvider can alert the user to more than one error at a time.
Summary. 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.
Also The ErrorProvider can streamline error corrections by presenting many errors at once.
© 2007-2021 sam allen. see site info on the changelog