![]() When the user enters and leaves a field, the events occur in the followingorder:Įnter (Occurs when a control is entered.) When the content of the field changes for any reason When the user attempts to leave the field These are appropriatereasons to perform field-level validations: If it is not, you candisplay an error to alert the user about the problem. Field-Level Validationįield-level validation ensures that the value entered in the field is inaccordance with the application's requirements. You can then use keystroke events at the control level toperform field-specific validations, such as restricting the field to onlynumeric digits. If you want to discardcertain types of characters at the form level, you can set the Handledproperty for the event argument to true (this does not allow the eventto propagate to the active control) otherwise, the events propagate to theactive control. Thisallows you to set up a two-tier validation on controls. When you set the KeyPreview property of a form to true, theform receives all three events KeyPress, KeyUp, and KeyDownjust before the active control receives these events. The Form object also has the KeyPress, KeyUp, and KeyDown events, but they are fired only when all the controls on theform are either hidden or disabled. Returns true if the Shift key is pressed otherwise, returns false.īy default, only the active control receives the keystroke events. Returns the modifier flags that indicate what combination of modifier keys (Ctrl, Shift, and Alt) are pressed. Returns the integer representation of the KeyData property. Returns the key code for the pressed key, along with modifier flags that indicate what combination of modifier keys (Ctrl, Shift, and Alt) are pressed at the same time. Its value is one of the values specified in the Keys enumeration. Indicates whether the event has been handled. Returns true if the Ctrl key is pressed otherwise, returns false. Returns true if the Alt key is pressed otherwise, returns false. Table 3.5 Important Members of the KeyEventArgs Class Event handlers of these eventsreceive an argument of KeyEventArgs type it provides the propertieslisted in Table 3.5. The KeyDown and KeyUp events occur when the user pressesand releases a key on the keyboard, respectively. To handle keypresses for function keys,control keys, and cursor movement keys, you must use the KeyDown and KeyUp events. The KeyPress event fires only if the key that ispressed generates a character value. Returns the character value that corresponds to the key Indicates whether the event has been handled Table 3.4 Important Members of the KeyPressEventArgs Class ![]() Table 3.4 lists the properties of KeyPressEventArgs. Its event handler receives an argument of type KeyPressEventArgs. The KeyPress event happens after the KeyDown event butbefore the KeyUp event. You choose the event to program based on the orderin which the event is fired and the information that is passed in the eventargument of the event handler. You can program the event handlers for these events in order to performkeystroke-level validation. When you press a key on a control, three events take place, in the followingorder: The first technique is discussed relative to the use of various controls inChapter 2, "Controls" the following sections cover rest of thesetechniques. You can analyze the contents of the data field as a whole and warn theuser of any incorrect values when he or she attempts to leave the field or closethe window. You can restrict entry in some data fields by enabling or disabling them,depending on the state of other fields. You can capture the user's keystrokes and analyze them for validity.Some fields may require the user to enter only alphabetic values but no numericvalues or special characters in that case, you can accept the keystrokes foralphabetic characters while rejecting others. Theseallow users to select from a set of given values rather than permit freekeyboard entry. You can restrict the values that a field can accept by using standardcontrols such as combo boxes, list boxes, radio buttons, and check boxes. You can use various techniques for validatingdata: The most relevant place to ensure the validity of data is at thetime of data entry itself. When designing an application that acceptsdata from the user, you must ensure that the entered data is acceptable for theapplication. MCAD/MCSD Training Guide (70-316): Developing and Implementing Windows-Based Applications with Visual C# and Visual Studio.NET
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |