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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Keyboard Events, Delegates and Event Arguments<br />

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

KeyCode Returns the key code for the key as a Keys enumeration.<br />

This does not include modifier-key information.<br />

Used <strong>to</strong> test for a specific key.<br />

KeyData Returns the key code for a key as a Keys enumeration,<br />

combined with modifier information. Contains<br />

all information about the pressed key.<br />

KeyValue Returns the key code as an Integer, rather than as a<br />

Keys enumeration. Used <strong>to</strong> obtain a numeric representation<br />

of the pressed key.<br />

Modifiers Returns a Keys enumeration for any modifier keys<br />

pressed (Alt, Control and Shift). Used <strong>to</strong> determine<br />

modifier-key information only.<br />

Fig. 12.31 Keyboard events, delegates and event arguments (part 2 of 2).<br />

Figure 12.32 demonstrates the use of the key-event handlers <strong>to</strong> display a key pressed<br />

by a user. The program is a form with two Labels. It displays the pressed key on one<br />

Label and modifier information on the other.<br />

Initially, the two Labels (lblCharacter and lblInformation) are empty. The<br />

lblCharacter label displays the character value of the key pressed, whereas lblInformation<br />

displays information relating <strong>to</strong> the pressed key. Because the KeyDown and<br />

KeyPress events convey different information, the form (FrmKeyDemo) handles both.<br />

The KeyPress event handler (lines 18–23) accesses the KeyChar property of the<br />

KeyPressEventArgs object. This returns the pressed key as a Char and displays the<br />

result in lblCharacter (line 22). If the pressed key is not an ASCII character, then the<br />

KeyPress event will not fire, and lblCharacter remains empty. ASCII is a common<br />

encoding format for letters, numbers, punctuation marks and other characters. It does not support<br />

keys such as the function keys (like F1) or the modifier keys (Alt, Control and Shift).<br />

1 ' Fig. 12.32: KeyDemo.vb<br />

2 ' Displaying information about a user-pressed key.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Public Class FrmKeyDemo<br />

7 Inherits Form<br />

8<br />

9 ' KeyPressEventArgs display label<br />

10 Friend WithEvents lblCharacter As Label<br />

11<br />

12 ' KeyEventArgs display label<br />

13 Friend WithEvents lblInformation As Label<br />

14<br />

Fig. 12.32 Demonstrating keyboard events (part 1 of 3).

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

Saved successfully!

Ooh no, something went wrong!