In most applications, the user enters information for the application through the user interface. Data validation ensures that all data entered by a user falls within acceptable parameters before proceeding with program execution. For example, you might have a field where a user enters a zip code as part of an address.
Using validation, you could verify that the field contained five and only five characters, all of which were numeric, before proceeding. Validating user input reduces the chance of an input error and makes your application more robust.
You can choose between two different types of validation for user input: form-level validation and field-level validation. Form-level validation verifies data after the user has filled all of the fields. For example, a user might be directed to fill in a name, address, and phone number on a form and then click OK. With form-level validation, all of the fields on the form would be validated when the user clicked OK.
Field-level validation, on the other hand, verifies the data in each field as the field is filled in. For example, if a user fills in a field that holds a phone number, field-level validation could verify that the number contains a valid area code before moving to the next field. As each digit is entered, you could also use control events to verify that only numbers are entered.
Field-Level Validation:
You might want to validate data as it is entered into each field. Field-level validation gives the developer control over user input as it occurs. In this section, you will learn how to use control events to validate user input, and how to use some properties of the TextBox control to help restrict input to appropriate parameters.
Using TextBox Properties:
The most commonly used control for user input is the TextBox. Several properties of the TextBox control allow you to restrict the values of user input that they will accept. Some of these properties include
- MaxLength
- PasswordChar
- ReadOnly
- MultiLine
Setting the MaxLength Property:
The MaxLength property limits the number of characters that can be entered into a text box. If the user attempts to exceed the number returned by MaxLength, the text box will accept no further input, and the system beeps to alert the user. This property is useful for text boxes that always contain data of the same length, such as a zip code field.
Using the PasswordChar Property:
The PasswordChar property allows you to hide user input at run time. For example, if you set the PasswordChar property to an asterisk (*), the text box will display an asterisk for each character regardless of user input. This type of behavior is commonly seen in password login boxes.
Although the password character is most commonly an asterisk, you can set it to any valid character. Thus, you could display all semicolons or ampersands, for example. The value of the Text property will always be set to the value the user enters regardless of the password character.
Setting the ReadOnly Property:
The ReadOnly property determines whether a user can edit the value displayed in a text box. If ReadOnly is set to true, the text cannot be changed by user input. If ReadOnly is set to false, the user can edit the value normally.
Using the MultiLine Property:
The MultiLine property determines whether a text box can accept multiple lines. When set to true, the user can enter multiple lines in the text box, each separated by a carriage return. The individual lines are stored as an array of strings in the TextBox.Lines collection and can be accessed by their index.
Using Events in Field-Level Validation:
Field-level keyboard events allow you to immediately validate user input. Controls that can receive keyboard input raise three keyboard events. They are
- KeyDown
- KeyPress
- KeyUp
KeyDown and KeyUp
The KeyDown and KeyUp events are raised when a key is pressed and a key is released, respectively. The control that has the focus raises the event. When these events are raised, they package information about which key or combination of keys has been pressed or released in an instance of KeyEventArgs, a class that describes the key combination pressed. A method that handles the KeyDown or KeyUp event must include a KeyEventArgs parameter in its signature.
The KeyUp and KeyDown events are most commonly used for determining if the Alt, Ctrl, or Shift key has been pressed. This information is exposed through properties in the KeyEventArgs reference that is passed to the handler.
The KeyEventArgs properties—Alt, Control, and Shift—are properties that return a Boolean value, which indicates whether or not those keys are down. A value of true is returned if the corresponding key is down, and false is returned if the key is up. The following example demonstrates a KeyUp event handler that checks whether the Alt key is pressed.
You can also use the KeyEventArgs.KeyCode property to examine the actual key that triggered the event. This property returns a Key value that represents the key that was pressed (in the case of a KeyDown event) or released (in the case of a KeyUp event).
When a user presses a key that has a corresponding ASCII value, the KeyPress event is raised. These keys include any alphabetic and numeric characters (alphanumeric a-z, A-Z, and 0-9) as well as some special keyboard characters, such as the Enter and Backspace keys. If a key or key combination does not produce an ASCII value, it will not raise the KeyPress event. Examples of keys that do not raise this event include Ctrl, Alt, and the function keys.
This event is most useful for intercepting keystrokes and evaluating them. When this event is raised, an instance of KeyPressEventArgs is passed as a parameter to the event handler. The KeyPressEventArgs.KeyChar property contains the ASCII character represented by the keystroke that raised the event. If you want to make sure that the key pressed is a numeric key, for example, you can evaluate the KeyChar property in your KeyPress event handler.
Validating Characters
The Char data type contains several Shared (static) methods that are useful for validating characters trapped by the KeyPress event. These methods include
- Char.IsDigit
- Char.IsLetter
- Char.IsLetterOrDigit
- Char.IsPunctuation
- Char.IsLower
- Char.IsUpper
Each of these methods evaluates a character and returns a Boolean value; they are fairly self-explanatory. The Char.IsDigit function returns true if a character is a numeric digit, false if it is not. The Char.IsLower function returns true if a character is a lowercase letter, false otherwise. The other methods behave similarly. The following example uses the Char.IsNumber method to test if the key pressed is a numeric key.
Handling the Focus
Focus is the ability of an object to receive user input through the mouse or keyboard. Although you can have several controls on your form, only one can have the focus at any given time. The control that has the focus is always on the active form of the application.
Every control implements the Focus method. This method sets the focus to the control that called it. The Focus method returns a Boolean value that indicates whether or not the control was successful in setting the focus. Controls that are disabled or invisible cannot receive the focus. You can determine if a control can receive the focus by checking the CanFocus property, which returns true if the control can receive the focus and false if the control cannot.
Focus events occur in the following order:
- Enter
- GotFocus
- Leave
- Validating
- Validated
- LostFocus
The Enter and Leave events are raised when the focus arrives at a control and when the focus leaves a control, respectively. GotFocus and LostFocus are raised when a control first obtains the focus and when the focus has been lost from the control, respectively. Although you can use these events for field-level validation, the Validating and Validated events are more suited to that task.
The Validating and Validated Events
The easiest way to validate data is to use the Validating event. The Validating event occurs before a control loses the focus. This event is raised only when the CausesValidation property of the control that is about to receive the focus is set to true. Thus, if you want to use the Validating event to validate data entered in your control, the CausesValidation of the next control in the tab order should be set to true. In order to use Validating events, the CausesValidation property of the control to be validated must also be set to true. By default, the CausesValidation property of all controls is set to true when controls are created at design time. Controls such as Help buttons are typically the only kind of controls that have CausesValidation set to false.
The Validating event allows you to perform sophisticated validation on your controls. You could, for example, implement an event handler that tested whether the value entered corresponded to a very specific format. Another possible use is an event handler that doesn't allow the focus to leave the control until a value has been entered.
The Validating event includes an instance of the CancelEventArgs class. This class contains a single property, Cancel. If the input in your control does not fall within required parameters, you can use the Cancel property within your event handler to cancel the Validating event and return the focus to the control.
The Validated event fires after a control has successfully been validated. You can use this event to perform any actions based upon the validated input.
The following example demonstrates a handler for the Validating event. This method requires an entry in TextBox1 before it will allow the focus to move to the next control.
DAY 1 MICROSOFT DOT NET FRAME WORK
DAY 2 MICROSOFT DOT NET BASE CLASS LIBRARY
DAY 3 MICROSOFT DOT NET CLASSES AND STRECTURES
DAY 4 METHODS IN FRAME WORK
DAY 5 INPUT VALIDATIONS IN DOT NET PART ONE
DAY 6 INPUT VALIDATIONS IN DOT NET PART TWO
DAY 7 DATA TYPES IN DOT NET
DAY 8 DATA TYPES IN DOT NET PART TWO
DAY 9 IMPLEMENTING PROPERTIES IN DOT NET
DAY 10 DELEGATES AND EVENTS
No comments:
Post a Comment