< Day Day Up > |
TechniqueControl validation uses a combination of the ErrorProvider control and control events. After designing the form, drag and drop an ErrorProvider control from the toolbox onto the form. To validate a control, create an event handler for the Validating and Validated events. Within the Validating event handler, perform any necessary validation for the control, such as empty string checks or regular expression matches. If the control data is not valid, call the SetError method defined in the ErrorProvider object instance, passing it the instance of the control that failed validation and a string message used as the tooltip of the error control. If you want to force a user to enter valid data before interacting with any other part of the form, set the Cancel property of the CancelEventArgs parameter passed in the Validating event handler. Any attempt to interact with another control or to close the form before valid data is entered, however, will fail, which might or might not be in line with your customer-experience design goals. Listing 8.11 Validating Form Inputprivate void tbLastName_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if( tbLastName.Text == "" ) { // setting e.Cancel to true requires user to // fill in field before continuing e.Cancel = true; errorProvider1.SetError( tbLastName, "Please enter a last name" ); } } private void tbFirstName_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if( tbFirstName.Text == "" ) { // set error field but allow user to set focus to other controls errorProvider1.SetError( tbFirstName, "Please enter a first name" ); } } If the Cancel property is not set to true , then the Validated event handler is called next. You should therefore perform the same validation step and set the ErrorProvider object accordingly . If Cancel is set to true , however, then you are assured that the Validated event is only fired when the control contains valid data. You should clear the error within the ErrorProvider for the valid control by calling the SetError method as before, passing an empty string as the string parameter. Listing 8.12 Displaying Errors with ErrorProviderprivate void tbPhone_Validated(object sender, System.EventArgs e) { Regex phoneExp = new Regex( @"^\(\d{3}\)\s\d{3}-\d{4}$" ); if( tbPhone.Text == "" ) { errorProvider1.SetError( tbPhone, "Please enter a phone number" ); } else if( phoneExp.Match( tbPhone.Text ).Success == false ) { errorProvider1.SetError( tbPhone, "Invalid Phone Number" ); } else { errorProvider1.SetError( tbPhone, "" ); } } private void tbFirstName_Validated(object sender, System.EventArgs e) { // clear error if they filled in first name if( tbFirstName.Text != "" ) errorProvider1.SetError( tbFirstName, "" ); } private void tbLastName_Validated(object sender, System.EventArgs e) { // clear error since control is validated errorProvider1.SetError( tbLastName, "" ); } You can also force form validation to occur at any time, such as at a button click, by calling the Validate method, which is defined in the System.Windows.Forms base class of your form. CommentsA large issue with allowing users to enter data manually into a control is that you aren't guaranteed that the data they enter is in the format your application can use. For instance, if you create a TextBox control and ask the user to enter his phone number, he might inadvertently insert a letter or skip a few digits. It is imperative that you utilize control validation to ensure this mistake doesn't happen. The ErrorProvider control within .NET is a control designed to automatically alert the user when a control fails validation. The ErrorProvider doesn't perform any validation itself, which means it's still up to you to write the validation code. However, it does free you from having to write any necessary custom code to display an error. When a validation error does occur, as shown in the "Technique" section, the SetError method of the ErrorProvider is called. The control that failed validation is then flagged with an icon, which by default is located to the right side of the control and which blinks on and off for a small period of time to alert the user. When the user hovers the mouse cursor over the icon, a tooltip explains the error. Just as every other control, the ErrorProvider contains several different properties that allow you to customize its display. For instance, you can display a custom icon by changing the Icon parameter or change the speed at which the icon blinks by changing the BlinkRate property. |
< Day Day Up > |