Partilhar via


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

  • 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.set_Text("&Print");
    
    // Set the letter "P" as an access key.
    button1->Text = "&Print";
    
    NoteNote

    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.

See Also

Tasks

How to: Respond to Windows Forms Button Clicks
How to: Set the Text Displayed by a Windows Forms Control

Reference

Button

Other Resources

Labeling Individual Windows Forms Controls and Providing Shortcuts to Them