Language

MenuItem.Checked プロパティ

定義

メニュー項目のテキストの横にチェック マークが表示されるかどうかを示す値を取得または設定します。

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 コントロール内のテキストの色を指定します。 提供されるイベント ハンドラーは、3 つのメニュー項目の 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 プロパティを使用して、相互に排他的なメニュー項目のグループ内の選択したメニュー項目を識別することもできます。

Note

このプロパティは、最上位のメニュー項目の true に設定できません。

適用対象