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
。