MenuItem.Checked 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,通过该值指示选中标记是否出现在菜单项文本的旁边。
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
;否则为 false
。 默认值为 false
。
例外
MenuItem 是顶级菜单,或有子级。
示例
下面的代码示例使用 Checked 属性在应用程序中提供状态。 在此示例中,一组菜单项用于指定控件中 TextBox 文本的颜色。 提供的事件处理程序由 Click 三个菜单项的事件使用。 每个菜单项指定一种文本颜色, menuItemRed
(红色) 、 menuItemGreen
(绿色) 或 menuItemBlue
(蓝色) 。 事件处理程序确定单击了哪个菜单项,在所选菜单项上放置复选标记,并更改窗体 TextBox 控件的文本颜色。 该示例要求 System.Drawing 命名空间已添加到此代码所在的窗体中。 该示例还要求 TextBox 已将 添加到此示例代码所在的窗体中,名为 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 属性与菜单中的其他菜单项结合使用,为应用程序提供状态。 例如,可以在一组项的菜单项上放置一个检查标记,以标识要为应用程序中的文本显示的字体大小。 还可以使用 Checked 属性来标识一组互斥菜单项中的所选菜单项。
注意
对于顶级菜单项,此属性不能设置为 true
。