MenuItem.Checked Eigenschaft

Definition

Ruft einen Wert ab, der angibt, ob neben dem Text des Menüelements ein Häkchen angezeigt wird, oder legt diesen fest.

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

Eigenschaftswert

true, wenn das Menüelement mit einem Häkchen versehen ist, andernfalls false. Der Standardwert ist false.

Ausnahmen

Das MenuItem ist ein Menü der obersten Ebene oder verfügt über untergeordnete Elemente.

Beispiele

Im folgenden Codebeispiel wird die Checked -Eigenschaft verwendet, um den Zustand in einer Anwendung bereitzustellen. In diesem Beispiel wird eine Gruppe von Menüelementen verwendet, um die Farbe für den Text in einem TextBox Steuerelement anzugeben. Der bereitgestellte Ereignishandler wird vom Click Ereignis von drei Menüelementen verwendet. Jedes Menüelement gibt eine Textfarbe menuItemRed (Rot), menuItemGreen (Grün) oder menuItemBlue (Blau) an. Der Ereignishandler bestimmt, auf welches Menüelement geklickt wurde, setzt ein Häkchen auf das ausgewählte Menüelement und ändert die Textfarbe des Formularsteuerelements TextBox . Das Beispiel erfordert, dass der System.Drawing Namespace dem Formular hinzugefügt wurde, in dem dieser Code platziert ist. Das Beispiel erfordert auch, dass dem TextBox Formular, in dem sich dieser Beispielcode befindet, mit dem Namen textBox1hinzugefügt wurde.

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

Hinweise

Sie können die Checked -Eigenschaft in Kombination mit anderen Menüelementen in einem Menü verwenden, um den Zustand für eine Anwendung anzugeben. Beispielsweise können Sie ein Häkchen auf einem Menüelement in einer Gruppe von Elementen setzen, um die Größe der Schriftart zu identifizieren, die für den Text in einer Anwendung angezeigt werden soll. Sie können die Checked -Eigenschaft auch verwenden, um das ausgewählte Menüelement in einer Gruppe von sich gegenseitig ausschließenden Menüelementen zu identifizieren.

Hinweis

Diese Eigenschaft kann für Menüelemente der obersten Ebene nicht auf true festgelegt werden.

Gilt für: