How to: Create access keys for Windows Forms controls

An access key is an underlined character in the text of a menu, menu item, or the label of a control such as a button. With an access key, the user can "click" a button by pressing the Alt key in combination with the predefined access key. For example, if a button runs a procedure to print a form, and therefore its Text property is set to "Print," adding an ampersand before the letter "P" causes the letter "P" to be underlined in the button text at run time. The user can run the command associated with the button by pressing Alt+P.

Controls that cannot receive focus can't have access keys.

Programmatic

Set the Text property to a string that includes an ampersand (&) before the letter that will be the shortcut.

' Set the letter "P" as an access key.
Button1.Text = "&Print"
// Set the letter "P" as an access key.
button1.Text = "&Print";
// Set the letter "P" as an access key.
button1->Text = "&Print";

Note

To use an ampersand in a caption without creating an access key, include two ampersands (&&). A single ampersand is displayed in the caption and no characters are underlined.

Designer

In the Properties window of Visual Studio, set the Text property to a string that includes an ampersand ('&') before the letter that will be the access key. For example, to set the letter "P" as the access key, enter &Print.

See also