C# Programmer[ap]s Cookbook

Problem

You need to alert the user of invalid input in a control, such as a TextBox .

Solution

Use the ErrorProvider control to display an error icon next to the offending control. Check for errors before allowing the user to continue.

Discussion

There are a number of ways that you can perform validation in a Windows- based application. One approach is to respond to control validation events and prevent users from changing focus from one control to another if an error exists. A less invasive approach is to simply flag the offending control in some way so that the user can review all the errors at once. You can use this approach in .NET with the ErrorProvider control.

The ErrorProvider is a special provider control that displays error icons next to invalid controls. You show the error icon next to a control by using the ErrorProvider.SetError method and specifying the appropriate control and a string error message. The ErrorProvider will then show a warning icon automatically to the right of the control. When the user hovers the mouse above the warning icon, the detailed message appears. Figure 6.10 shows how the ErrorProvider will indicate an input error for a TextBox control.

Figure 6.10: A validated form with the ErrorProvider .

You only need to add one ErrorProvider control to your form, and you can use it to display an error icon next to any control. To add the ErrorProvider , drag it into the component tray or create it manually in code. The following form code checks the content of the text box every time a key is pressed. The code validates this text box using a regular expression with checks to see if the value corresponds to a valid e-mail address. If validation fails, the ErrorProvider is used to display an error message. If the text is valid, any existing error message is cleared from the ErrorProvider . Finally, the Click event handler for the OK button steps through all the controls on the form and verifies that none of them have errors before allowing the application to continue.

using System; using System.Windows.Forms; using System.Text.RegularExpressions; public class ErrorProviderValidation : System.Windows.Forms.Form { // (Designer code omitted.) private void txtEmail_TextChanged(object sender, System.EventArgs e) { Regex regex; regex = new Regex(@"\S+@\S+\.\S+"); Control ctrl = (Control)sender; if (regex.IsMatch(ctrl.Text)) { errProvider.SetError(ctrl, ""); } else { errProvider.SetError(ctrl, "This is not a valid e-mail address."); } } private void cmdOK_Click(object sender, System.EventArgs e) { string errorText = ""; bool invalidInput = false; foreach (Control ctrl in this.Controls) { if (errProvider.GetError(ctrl) != "") { errorText += " * " + errProvider.GetError(ctrl) + "\n"; invalidInput = true; } } if (invalidInput) { MessageBox.Show( "The form contains the following unresolved errors:\n\n" + errorText, "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { this.Close(); } } }

Категории