MenuItem.Checked 属性

定义

获取或设置一个值,通过该值指示选中标记是否出现在菜单项文本的旁边。Gets or sets a value indicating whether a check mark appears next to the text of the menu item.

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

属性值

如果菜单项旁边有一个选中标记,则为 true;否则为 falsetrue if there is a check mark next to the menu item; otherwise, false. 默认值为 falseThe default is false.

异常

MenuItem 是顶级菜单,或有子级。The MenuItem is a top-level menu or has children.

示例

下面的代码示例使用 Checked 属性来提供应用程序中的状态。The following code example uses the Checked property to provide the state in an application. 在此示例中,一组菜单项用于指定 TextBox 控件中的文本的颜色。In this example, a group of menu items are used to specify the color for the text in a TextBox control. 提供的事件处理程序由三个菜单项的 Click 事件使用。The event handler provided is used by the Click event of three menu items. 每个菜单项指定文本颜色、menuItemRed (红色)、menuItemGreen (绿色)或 menuItemBlue (蓝色)。Each menu item specifies a text color, menuItemRed (Red), menuItemGreen (Green), or menuItemBlue (Blue). 事件处理程序确定单击的菜单项,将复选标记置于所选菜单项上,并更改窗体 TextBox 控件的文本颜色。The event handler determines which menu item was clicked, places a checkmark to the selected menu item, and changes the text color of the form's TextBox control. 该示例要求将 System.Drawing 命名空间添加到了此代码所放置到的窗体中。The example requires that the System.Drawing namespace has been added to the form that this code is placed in. 该示例还要求将 TextBox 添加到此示例代码所在的窗体中,该窗体名为 textBox1The example also requires that a TextBox has been added to the form that this example code is located in that is named textBox1.

private:
   // The following event handler would be connected to three menu items.
   void MyMenuClick( Object^ sender, EventArgs^ e )
   {
      // Determine if clicked menu item is the Blue menu item.
      if ( sender == menuItemBlue )
      {
         // Set the checkmark for the menuItemBlue menu item.
         menuItemBlue->Checked = true;
         // Uncheck the menuItemRed and menuItemGreen menu items.
         menuItemRed->Checked = false;
         menuItemGreen->Checked = false;
         // Set the color of the text in the TextBox control to Blue.
         textBox1->ForeColor = Color::Blue;
      }
      else if ( sender == menuItemRed )
      {
         
         // Set the checkmark for the menuItemRed menu item.
         menuItemRed->Checked = true;
         // Uncheck the menuItemBlue and menuItemGreen menu items.
         menuItemBlue->Checked = false;
         menuItemGreen->Checked = false;
         // Set the color of the text in the TextBox control to Red.
         textBox1->ForeColor = Color::Red;
      }
      else
      {
         // Set the checkmark for the menuItemGreen.
         menuItemGreen->Checked = true;
         // Uncheck the menuItemRed and menuItemBlue menu items.
         menuItemBlue->Checked = false;
         menuItemRed->Checked = false;
         // Set the color of the text in the TextBox control to Blue.
         textBox1->ForeColor = Color::Green;
      }
   }
// The following event handler would be connected to three menu items.
 private void MyMenuClick(Object sender, EventArgs e)
 {
    // Determine if clicked menu item is the Blue menu item.
    if(sender == menuItemBlue)
    {
       // Set the checkmark for the menuItemBlue menu item.
       menuItemBlue.Checked = true;
       // Uncheck the menuItemRed and menuItemGreen menu items.
       menuItemRed.Checked = false;
       menuItemGreen.Checked = false;
       // Set the color of the text in the TextBox control to Blue.
       textBox1.ForeColor = Color.Blue;
    }
    else if(sender == menuItemRed)
    {
       // Set the checkmark for the menuItemRed menu item.
       menuItemRed.Checked = true;
       // Uncheck the menuItemBlue and menuItemGreen menu items.
       menuItemBlue.Checked = false;
       menuItemGreen.Checked = false;
       // Set the color of the text in the TextBox control to Red.
       textBox1.ForeColor = Color.Red;
    }
    else
    {
       // Set the checkmark for the menuItemGreen.
       menuItemGreen.Checked = true;
       // Uncheck the menuItemRed and menuItemBlue menu items.
       menuItemBlue.Checked = false;
       menuItemRed.Checked = false;
       // Set the color of the text in the TextBox control to Blue.
       textBox1.ForeColor = Color.Green;
    }
 }

' The following event handler would be connected to three menu items.
Private Sub MyMenuClick(sender As Object, e As EventArgs)
    ' Determine if clicked menu item is the Blue menu item.
    If sender Is menuItemBlue Then
        ' Set the checkmark for the menuItemBlue menu item.
        menuItemBlue.Checked = True
        ' Uncheck the menuItemRed and menuItemGreen menu items.
        menuItemRed.Checked = False
        menuItemGreen.Checked = False
        ' Set the color of the text in the TextBox control to Blue.
        textBox1.ForeColor = Color.Blue
    Else
        If sender Is menuItemRed Then
            ' Set the checkmark for the menuItemRed menu item.
            menuItemRed.Checked = True
            ' Uncheck the menuItemBlue and menuItemGreen menu items.
            menuItemBlue.Checked = False
            menuItemGreen.Checked = False
            ' Set the color of the text in the TextBox control to Red.
            textBox1.ForeColor = Color.Red
        Else
            ' Set the checkmark for the menuItemGreen.
            menuItemGreen.Checked = True
            ' Uncheck the menuItemRed and menuItemBlue menu items.
            menuItemBlue.Checked = False
            menuItemRed.Checked = False
            ' Set the color of the text in the TextBox control to Blue.
            textBox1.ForeColor = Color.Green
        End If
    End If
End Sub

注解

可以将 Checked 属性与菜单中的其他菜单项结合使用,以提供应用程序的状态。You can use the Checked property in combination with other menu items in a menu to provide state for an application. 例如,您可以对一组项中的菜单项设置一个选中标记,以确定要为应用程序中的文本显示的字体大小。For example, you can place a check mark on a menu item in a group of items to identify the size of the font to be displayed for the text in an application. 你还可以使用 Checked 属性来标识一组互斥菜单项中的选定菜单项。You can also use the Checked property to identify the selected menu item in a group of mutually exclusive menu items.

备注

对于顶级菜单项,不能将此属性设置为 trueThis property cannot be set to true for top-level menu items.

适用于