Udostępnij za pośrednictwem


How to: Create Access Keys with Windows Forms Label Controls

Windows Forms Label controls can be used to define access keys for other controls. When you define an access key in a label control, the user can press the ALT key plus the character you designate to move the focus to the control that follows it in the tab order. Because labels cannot receive focus, focus automatically moves to the next control in the tab order. Use this technique to assign access keys to text boxes, combo boxes, list boxes, and data grids.

To assign an access key to a control with a label

  1. Draw the label first, and then draw the other control.

    -or-

    Draw the controls in any order and set the TabIndex property of the label to one less than the other control.

  2. Set the label's UseMnemonic property to true.

  3. Use an ampersand (&) in the label's Text property to assign the access key for the label. For more information, see Creating Access Keys for Windows Forms Controls.

    Note

    You may want to display ampersands in a label control, rather than use them to create access keys. This may occur if you bind a label control to a field in a recordset where the data includes ampersands. To display ampersands in a label control, set the UseMnemonic property to false. If you wish to display ampersands and also have an access key, set the UseMnemonic property to true and indicate the access key with one ampersand (&) and the ampersand to display with two ampersands.

    Label1.UseMnemonic = True
    Label1.Text = "&Print"
    Label2.UseMnemonic = True
    Label2.Text = "&Copy && Paste"
    
    label1.UseMnemonic = true;
    label1.Text = "&Print";
    label2.UseMnemonic = true;
    label2.Text = "&Copy && Paste";
    
    label1.set_UseMnemonic(true);
    label1.set_Text("&Print");
    label2.set_UseMnemonic(true);
    label2.set_Text("&Copy && Paste");
    
    label1->UseMnemonic = true;
    label1->Text = "&Print";
    label2->UseMnemonic = true;
    label2->Text = "&Copy && Paste";
    

See Also

Tasks

How to: Size a Windows Forms Label Control to Fit Its Contents

Reference

Label Control Overview (Windows Forms)

Other Resources

Label Control (Windows Forms)