Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.

Viewing the Key Entered: The KeyPress Event

In many cases, you may want to inspect the key that the user enters into a control for various purposes. For example, you may want your program to perform a series of actions when the user presses the Enter key. Or, you may want to ensure that only certain types of keys, such as numeric keys, are entered into a text box. When the user presses a key, the control’s KeyPress event is triggered. This event occurs after a key is pressed, but before the key is shown in the control. When the event is triggered, the system also passes a structure—the event argument object—that contains the KeyChar and Handled properties. The KeyChar property gives the key pressed by the user and the Handled property allows you to indicate whether you have handled the key in the event procedure. You can inspect the KeyChar and perform properly actions. For example, suppose you want to ensure that the user does not enter a question mark in a text box named txtAddress. Your code will most likely appear as follows:

Private Sub txtAddress_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAddress.KeyPress If e.KeyChar = "?" Then ' This is a question mark MsgBox("No question mark, please.") 'Display a message e.Handled = True 'Tell the system to suppress the key End If End Sub

In this event procedure, the key that the user presses (KeyChar) is passed to the procedure as a part of the event argument object, e (thus you code e.KeyChar refers to the key pressed) and is compared with the question mark (“?”). If they are equal, the message, “No question mark, please.” is displayed. In addition, the Handled property is set to True to tell the system that the event has been handled and not to process the key further. This, in effect, suppresses the key pressed because the key has not been, and will not be, displayed in the text box.
The KeyPress event procedure includes a KeyPressEventArgs parameter (named e). This parameter is different from that in the Click event or the CheckedChanged event. It is worth repeating that when coding an event procedure, you should always use the procedure template provided by VB rather than create your own event procedure header. All the parameters in an event procedure are predefined and are automatically provided in the procedure template. (You should never change any part of the parameter list.) This way, you will never have to figure out what parameters the event header should have. The steps to obtain a procedure template were explained in Chapter 2.

When you double-click a control on a form, a default event procedure template for the control is automatically shown in the code window. This provides a convenient shortcut to the code window and the event procedure. Be very careful when doing this. The event procedure template that is given to you this way may not be exactly the one you want. For example, when you double-click a text box, its TextChanged event procedure template will appear, but you may actually want to use the KeyPress event. Placing the correct code in a wrong event procedure unintentionally is a frequent error for beginners. Pay extra attention to avoid this type of mistake.
Last change: February 13 2016 18:47:28.