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. You cannot have an access key for a control that cannot receive focus.

To create an access key for a control

  1. 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";

    To include 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.