30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12 Graphical User Interface Concepts: Part 1 513<br />

event. Line 32 sets shouldPaint <strong>to</strong> True. Unlike MouseMove events, the system generates<br />

a MouseDown event only once while the mouse but<strong>to</strong>n is down.<br />

When the user releases the mouse but<strong>to</strong>n (<strong>to</strong> complete a “click” operation), the system<br />

generates a single MouseUp event. FrmPainter_MouseUp handles the MouseUp<br />

event (lines 36–41). Line 40 sets shouldPaint <strong>to</strong> False.<br />

12.10 Keyboard-Event Handling<br />

This section explains the handling of key events, which are generated when keys on the keyboard<br />

are pressed and released. Such events can be handled by any control that inherits<br />

from System.Windows.Forms.Control. There are two types of key events. The<br />

first is event KeyPress, which fires when a key representing an ASCII character is<br />

pressed (determined by KeyPressEventArgs property KeyChar). ASCII is a 128character<br />

set of alphanumeric symbols, a full listing of which can be found in Appendix E,<br />

ASCII Character Set.<br />

<strong>How</strong>ever the KeyPress event does not enable us <strong>to</strong> determine whether modifier keys<br />

(e.g., Shift, Alt and Control) were pressed. It is necessary <strong>to</strong> handle the second type of key<br />

events, the KeyUp or KeyDown events, <strong>to</strong> determine such actions. Class KeyEventArgs<br />

contains information about special modifier keys. The key’s Key enumeration value can<br />

be returned, providing information about a wide range of non-ASCII keys. Often, modifier<br />

keys are used in conjunction with the mouse <strong>to</strong> select or highlight information.<br />

KeyEventHandler (event argument class KeyEventArgs) and KeyPress–<br />

EventHandler (event argument class KeyPressEventArgs) are the delegates for<br />

the two classes. Figure 12.31 lists important information about key events.<br />

Keyboard Events, Delegates and Event Arguments<br />

Key Events (Delegate KeyEventHandler, event arguments KeyEventArgs)<br />

KeyDown Generated when key is initially pressed.<br />

KeyUp Generated when key is released.<br />

Key Events (Delegate KeyPressEventHandler, event arguments KeyPressEventArgs)<br />

KeyPress Generated when key is pressed. Occurs repeatedly<br />

while key is held down, at a rate specified by the operating<br />

system.<br />

Class KeyPressEventArgs Properties<br />

KeyChar Returns the ASCII character for the key pressed.<br />

Handled Indicates whether the KeyPress event was handled.<br />

Class KeyEventArgs Properties<br />

Alt Indicates whether the Alt key was pressed.<br />

Control Indicates whether the Control key was pressed.<br />

Shift Indicates whether the Shift key was pressed.<br />

Fig. 12.31 Keyboard events, delegates and event arguments (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!