1. Technology

How to Display the & (ampersand) Character on Labels and Buttons

Why is "Me & You" displayed as "Me _You"?


By Windows design, accelerator keys enable the user to access a menu command from the keyboard by pressing Alt along the appropriate letter, indicated in your code by the preceding ampersand (&). The character after the and sign (&) appears underlined in the menu.

How to Specify the Shortcut (Accelerator) Key?

To specify "D" as the shortcut for the menu item with the caption "Me and Delphi" just place the "&" in front of "D", as in: "Me and &Delphi".

How to actually display the "&" character?

In the example above, if you want the caption to be "Me & Delphi" - where "and" is replaced by the and sign "&" - you need to use two and signs, as in:

"Me && Delphi"

By placing two ampesands together - you state that the character following the first one is *not* used as the accelerator - rather you actually want to get it (only one) displayed.

Note: "&&&" (or any similar combination) will *not* transfor the "&" sign into an accelerator.

The TLabel control and the ShowAccelChar and FocusControl properties

The "&" character has one more important role in designing the user interface.

Delphi's TLabel control exposes two interesting properties, FocusControl and ShowAccelChar.

When ShowAccelChar is true, any character preceded by an ampersand (&) appears underlined. If the FocusControl property is set, the windowed control specified by the FocusControl property receives input focus when the user types that underlined character.
When ShowAccelChar is false, the value of the FocusControl property is not used.

Delphi tips navigator:
» Parsing Command Line Parameters with Delphi
« Delphi's NIL value and the Assigned RTL Function

©2014 About.com. All rights reserved.