Freigeben über


MenuItem.Checked-Eigenschaft

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Property Checked As Boolean
'Usage
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)

Eigenschaftenwert

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

Ausnahmen

Ausnahmetyp Bedingung

ArgumentException

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

Hinweise

Sie können die Checked-Eigenschaft zusammen mit anderen Menüelementen in einem Menü verwenden, um den Zustand für eine Anwendung bereitzustellen. Sie können z. B. ein Häkchen in ein Menüelement in einer Gruppe von Elementen setzen, um die Größe der Schriftart für die Anzeige von Text in einer Anwendung zu bezeichnen. Mithilfe der Checked-Eigenschaft können Sie auch das ausgewählte Menüelement in einer Gruppe von sich gegenseitig ausschließenden Menüelementen kennzeichnen.

Hinweis

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

Beispiel

Im folgenden Codebeispiel wird mithilfe der Checked-Eigenschaft der Zustand in einer Anwendung bereitgestellt. In diesem Beispiel wird mithilfe einer Gruppe von Menüelementen die Textfarbe in einem TextBox-Steuerelement angegeben. Der bereitgestellte Ereignishandler wird von dem Click-Ereignis dreier Menüelemente verwendet. Jedes Menüelement gibt eine Textfarbe an: menuItemRed (Rot), menuItemGreen (Grün) oder menuItemBlue (Blau). Der Ereignishandler ermittelt, auf welches Menüelement geklickt wurde, setzt ein Häkchen neben das ausgewählte Menüelement und ändert die Textfarbe für das TextBox-Steuerelement des Formulars. Bei diesem Beispiel müssen dem Formular, in dem sich der Code befindet, zuvor der System.Drawing-Namespace und eine TextBox mit dem Namen textBox1 hinzugefügt werden.

' 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 

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

MenuItem-Klasse
MenuItem-Member
System.Windows.Forms-Namespace