JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Using Labels to Give Access Keys to Controls without Captions

The Testing Department is calling again: the old thorny issue of keyboard access has resurfaced. Theoretically, they say, users should be able to use your program, SuperDuperDataCrunch, with just the keyboard. Fine, you say; we can add access keys to all the button captions, so the user can give the button the focus just by pressing Alt and the access key (just like menu items). Don't forget to do the same to all the text boxes, the Testing Department says, and hangs up. You think: how do you give an access key to a text box?

This is where a useful aspect of labels comes in handy. In fact, this aspect of the label control is built just to handle this problem: you can give access keys to controls with Text properties that display caption text for the control (like buttons) just by placing an ampersand (&) in the caption in front of the letter you want to make the access key. The access key appears underlined and the corresponding control is given the focus if the user presses Alt and the access key—but how can you do that if a control (like a text box) uses the Text property to store user-editable text?

Here's the way you do it: you give the access key to a label control, and then make sure the control you want to give the focus to with that access key is next in the tab order (i.e., has the next highest TabIndex property value). Because labels cannot accept the focus themselves, this is a neat feature; when the user presses Alt and the access key, the label passes the focus on to the next control. In this way, you can give even controls like text boxes access keys.


When you use access keys, make sure you set the label's UseMnemonic property to True (the default), or the access key won't be enabled.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor