Sdílet prostřednictvím


MenuItem.Checked Vlastnost

Definice

Získá nebo nastaví hodnotu označující, zda se značka zaškrtnutí zobrazí vedle textu položky nabídky.

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

Hodnota vlastnosti

truepokud je vedle položky nabídky značka zaškrtnutí; v opačném případě . false Výchozí hodnota je false.

Výjimky

Jedná se MenuItem o nabídku nejvyšší úrovně nebo má podřízené položky.

Příklady

Následující příklad kódu používá Checked vlastnost k poskytnutí stavu v aplikaci. V tomto příkladu se používá skupina položek nabídky k určení barvy textu v ovládacím TextBox prvku. Poskytnutá obslužná rutina události se používá událostí Click tří položek nabídky. Každá položka nabídky určuje barvu textu ( menuItemRed červená), menuItemGreen (zelená) nebo menuItemBlue (modrá). Obslužná rutina události určuje, na kterou položku nabídky jste klikli, umístí značku zaškrtnutí na vybranou položku nabídky a změní barvu textu ovládacího prvku formuláře TextBox . Příklad vyžaduje, aby System.Drawing byl obor názvů přidán do formuláře, do kterého je tento kód umístěn. Příklad také vyžaduje, aby TextBox byl přidán do formuláře, že tento ukázkový kód je umístěn v tomto pojmenovaném 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

Poznámky

Vlastnost můžete použít Checked v kombinaci s dalšími položkami nabídky v nabídce a poskytnout tak stav aplikace. Můžete například umístit značku zaškrtnutí na položku nabídky ve skupině položek a určit velikost písma, které se má zobrazit pro text v aplikaci. Vlastnost můžete také použít Checked k identifikaci vybrané položky nabídky ve skupině vzájemně se vylučují položky nabídky.

Poznámka:

Tuto vlastnost nelze nastavit pro true položky nabídky nejvyšší úrovně.

Platí pro