Label.UseMnemonic Label.UseMnemonic Label.UseMnemonic Label.UseMnemonic Property

定义

获取或设置一个值,该值指示控件是否将控件的 Text 属性中的 & 符解释为访问键前缀字符。Gets or sets a value indicating whether the control interprets an ampersand character (&) in the control's Text property to be an access key prefix character.

public:
 property bool UseMnemonic { bool get(); void set(bool value); };
public bool UseMnemonic { get; set; }
member this.UseMnemonic : bool with get, set
Public Property UseMnemonic As Boolean

属性值

true如果标签显示的是 "&" 字符并在其显示的文本中的字符后面加上符号, 并将带下划线的字符视为访问密钥, 则为;否则, false如果在控件的文本中显示 "&" 字符, 则为。true if the label doesn't display the ampersand character and underlines the character after the ampersand in its displayed text and treats the underlined character as an access key; otherwise, false if the ampersand character is displayed in the text of the control. 默认值为 trueThe default is true.

示例

下面的代码示例演示如何创建一个Label控件, 该控件具有三维边框和ImageList使用和ImageIndex属性显示的图像。The following code example demonstrates how to create a Label control that has a three-dimensional border and an image displayed using the ImageList and ImageIndex properties. 该控件还具有一个标题, 其中指定了助记符。The control also has a caption with a mnemonic character specified. 示例代码使用PreferredHeightPreferredWidth属性在显示控件的窗体Label上正确调整控件的大小。The example code uses the PreferredHeight and PreferredWidth properties to properly size the Label control on the form on which it is displayed. 此示例要求ImageList已创建并命名为 imageList1, 并且已加载了两个图像。This example requires that an ImageList has been created and named imageList1 and that it has loaded two images. 该示例还要求代码位于将System.Drawing命名空间添加到其代码中的窗体中。The example also requires that the code is within a form that has the System.Drawing namespace added to its code.

public:
   void CreateMyLabel()
   {
      // Create an instance of a Label.
      Label^ label1 = gcnew Label;
      
      // Set the border to a three-dimensional border.
      label1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
      // Set the ImageList to use for displaying an image.
      label1->ImageList = imageList1;
      // Use the second image in imageList1.
      label1->ImageIndex = 1;
      // Align the image to the top left corner.
      label1->ImageAlign = ContentAlignment::TopLeft;
      
      // Specify that the text can display mnemonic characters.
      label1->UseMnemonic = true;
      // Set the text of the control and specify a mnemonic character.
      label1->Text = "First &Name:";
      
      /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
      label1->Size = System::Drawing::Size( label1->PreferredWidth, label1->PreferredHeight );
      
      //...Code to add the control to the form...
   }
public void CreateMyLabel()
{
   // Create an instance of a Label.
   Label label1 = new Label();

   // Set the border to a three-dimensional border.
   label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   // Set the ImageList to use for displaying an image.
   label1.ImageList = imageList1;
   // Use the second image in imageList1.
   label1.ImageIndex = 1;
   // Align the image to the top left corner.
   label1.ImageAlign = ContentAlignment.TopLeft;

   // Specify that the text can display mnemonic characters.
   label1.UseMnemonic = true;
   // Set the text of the control and specify a mnemonic character.
   label1.Text = "First &Name:";
   
   /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
   label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight);

   //...Code to add the control to the form...
}

Public Sub CreateMyLabel()
    ' Create an instance of a Label.
    Dim label1 As New Label()
       
    ' Set the border to a three-dimensional border.
    label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    ' Set the ImageList to use for displaying an image.
    label1.ImageList = imageList1
    ' Use the second image in imageList1.
    label1.ImageIndex = 1
    ' Align the image to the top left corner.
    label1.ImageAlign = ContentAlignment.TopLeft
     
    ' Specify that the text can display mnemonic characters.
    label1.UseMnemonic = True
    ' Set the text of the control and specify a mnemonic character.
    label1.Text = "First &Name:"
       
    ' Set the size of the control based on the PreferredHeight and PreferredWidth values. 
    label1.Size = New Size(label1.PreferredWidth, label1.PreferredHeight)

    '...Code to add the control to the form...
End Sub

注解

如果将true Text属性设置为, 并且在的Label属性中定义助记键字符 (前面跟 "and" 符的字符), 则按 ALT + 助记符将焦点设置到UseMnemonicLabel按 tab 键顺序排列。If the UseMnemonic property is set to true and a mnemonic character (a character preceded by the ampersand) is defined in the Text property of the Label, pressing ALT+ the mnemonic character sets the focus to the control that follows the Label in the tab order. 您可以使用此属性为窗体上的控件提供适当的键盘导航。You can use this property to provide proper keyboard navigation to the controls on your form.

适用于

另请参阅