MenuItem.Checked 属性

获取或设置一个值,通过该值指示选中标记是否出现在菜单项文本的旁边。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property Checked As Boolean
用法
Dim instance As MenuItem
Dim value As Boolean

value = instance.Checked

instance.Checked = value
public bool Checked { get; set; }
public:
property bool Checked {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_Checked ()

/** @property */
public void set_Checked (boolean value)
public function get Checked () : boolean

public function set Checked (value : boolean)

属性值

如果菜单项旁边有一个选中标记,则为 true;否则为 false。默认为 false

异常

异常类型 条件

ArgumentException

MenuItem 是顶级菜单,或有子级。

备注

可以使用 Checked 属性以及菜单中的其他菜单项为应用程序提供状态。例如,可以将选中标记放在一组项中的菜单项上,以标识应用程序中显示文本的字体大小。还可以使用 Checked 属性标识一组互斥的菜单项中选定的菜单项。

提示

无法为顶级菜单项将该属性设置为 true

示例

下面的代码示例使用 Checked 属性提供应用程序中的状态。在此示例中,使用了一组菜单项来为 TextBox 控件中的文本指定颜色。所提供的事件处理程序由三个菜单项的 Click 事件使用。每个菜单项指定一种文本颜色:menuItemRed 指定红色,menuItemGreen 指定绿色,menuItemBlue 指定蓝色。事件处理程序确定哪个菜单项被单击,将选中标记放在选定的菜单项上,并更改窗体的 TextBox 控件的文本颜色。此示例要求已将 System.Drawing 命名空间添加到放置该代码的窗体上。此示例还要求已将 TextBox 添加到该示例代码所在的窗体(名为 textBox1)上。

' 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
// 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;
    }
 }
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.Equals( menuItemBlue)) {
        // Set the checkmark for the menuItemBlue menu item.
        menuItemBlue.set_Checked(true);

        // Uncheck the menuItemRed and menuItemGreen menu items.
        menuItemRed.set_Checked(false);
        menuItemGreen.set_Checked(false);

        // Set the color of the text in the TextBox control to Blue.
        textBox1.set_ForeColor(Color.get_Blue());
    }
    else {
        if (sender.Equals( menuItemRed)) {
            // Set the checkmark for the menuItemRed menu item.
            menuItemRed.set_Checked(true);

            // Uncheck the menuItemBlue and menuItemGreen menu items.
            menuItemBlue.set_Checked(false);
            menuItemGreen.set_Checked(false);

            // Set the color of the text in the TextBox control to Red.
            textBox1.set_ForeColor(Color.get_Red());
        }
        else {
            // Set the checkmark for the menuItemGreen.
            menuItemGreen.set_Checked(true);

            // Uncheck the menuItemRed and menuItemBlue menu items.
            menuItemBlue.set_Checked(false);
            menuItemRed.set_Checked(false);

            // Set the color of the text in the TextBox control to Blue.
            textBox1.set_ForeColor(Color.get_Green());
        }
    }
} //MyMenuClick 

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

MenuItem 类
MenuItem 成员
System.Windows.Forms 命名空间