Microsfot dot net Input validations part 2

Form-Level Validation

Form-level validation is the process of validating all of the fields on a form at once. A central procedure is used to implement form-level validation and is usually called when the user is ready to proceed to another step. A more advanced method of form-level validation is implementing a form-level keyboard handler.

The following example demonstrates how to create a form-level validation method. The sample tests that all the text boxes on a form have received input when a button called btnValidate is pressed, and resets the focus to the first text box it encounters without input.

Form-Level Keyboard Handler

A keyboard handler is a somewhat more sophisticated technique for form-level validation. A centralized keyboard handler allows you to manage data input for all fields on a form. For example, you could create a method that enables command buttons only after appropriate input has been entered into each field, and performs specific actions with each keystroke.

The KeyPress, KeyDown, and KeyUp events are used to implement a form-level keyboard handler. If a form has no visible and enabled controls, it will automatically raise keyboard events. If there are any controls on the form, however, the form will not automatically raise these events. In order for the form to raise these events, the KeyPreview property of the form must be set to true. When set to true, the form will raise keystroke events before the control that has the focus. For example, assume that there is a KeyPress handler for the form, a KeyPress handler for a text box on that form, and that the KeyPreview property of the form is set to true. When a key is pressed, the form will raise the KeyPress event first, and the form's KeyPress event handler will execute first. When execution has completed, the text box's KeyPress event handler will execute.

Providing User Feedback

When invalid input is entered into a field, the user should be alerted and given an opportunity to correct the error. There are many ways to inform the user of an input error. If the error is obvious and self-explanatory, an audio cue can alert the user to the problem. In Visual Basic .NET, you can use the Beep method to produce an attention-getting sound.

Other ways to draw a user's attention to an error include changing the BackColor or ForeColor of a control. For example, a text box with invalid input might have its BackColor changed to red.

If more detailed messages are required, you can use the MessageBox.Show method. This method displays a small, modal dialog box that contains an informative message. Because it is displayed modally, it halts program execution and is impossible for the user to ignore. The following shows how to call the MessageBox.Show method, which includes an informative message.

The ErrorProvider Component

The ErrorProvider component provides an easy way to communicate validation errors to your users. The ErrorProvider allows you to set an error message for each control on your form when the input is not valid. When an error message is set, an icon indicating the error will appear next to the control, and the error message text will be shown as a Tool Tip when the mouse hovers over the affected control. The ErrorProvider component can be found in the Windows Forms tab of the Toolbox.

Displaying an Error

To cause an error condition to be displayed next to a control, you use the SetError method of the ErrorProvider component. The SetError method requires the name of the control to be set and the text to be provided.

You can also set an error at design time. In the Properties window, you will find that once you add an ErrorProvider control to your form, each control has a new property called Error on x where x is the name of the ErrorProvider. You can set this property to a value at design time in the Properties window. If a value is set for the error, the control will immediately show an error at run time.

Different properties of the ErrorProvider component affect how the information is displayed to the user. The Icon property controls which icon is displayed next to the control. You might want to have multiple error providers on a single form—one that reports errors and one that reports warnings. You could use different icons for each to provide visual cues to the user. Another property is the BlinkStyle property. This property determines whether or not the error icon blinks when displayed. The BlinkRate property determines how rapidly the icon blinks.

To create a validation handler that uses the ErrorProvider component

  1. Create your form and add an ErrorProvider component to your form.

    The ErrorProvider component appears in the component tray.

  2. Set the CausesValidation property of the control you want to provide errors for to true if it is not true already.
  3. In the event handler for that control's Validating event, test the value. If an error condition occurs, use the SetError method to set the error to be displayed. The following example demonstrates a validation handler for a text box named pswordTextBox and an erroR provider named myErrorProvider.
related post

No comments:

Post a Comment