MenuItem.Checked Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Recebe ou define um valor que indica se aparece uma marca de verificação ao lado do texto do item do menu.
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
Valor de Propriedade
true se houver um visto de seleção ao lado do item do menu; caso contrário, false. A predefinição é false.
Exceções
Existe MenuItem um menu de nível superior ou tem crianças.
Exemplos
O exemplo de código seguinte utiliza a Checked propriedade para fornecer o estado numa aplicação. Neste exemplo, um grupo de itens do menu é usado para especificar a cor do texto num TextBox controlo. O gestor de eventos fornecido é usado pelo Click evento de três itens do menu. Cada item do menu especifica uma cor de texto, menuItemRed (Vermelho), menuItemGreen (Verde) ou menuItemBlue (Azul). O gestor de eventos determina qual item do menu foi clicado, coloca uma marca de seleção no elemento selecionado e altera a cor do texto do controlo do TextBox formulário. O exemplo exige que o System.Drawing namespace tenha sido adicionado ao formulário onde este código está colocado. O exemplo também exige que a TextBox tenha sido adicionado à forma em que este código de exemplo está localizado, que se chama 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
Observações
Pode usar a Checked propriedade em combinação com outros itens do menu para fornecer o estado de uma aplicação. Por exemplo, pode colocar uma marca de verificação num item do menu num grupo de itens para identificar o tamanho da fonte a ser exibida para o texto numa aplicação. Também pode usar a Checked propriedade para identificar o item selecionado do menu num grupo de itens do menu mutuamente exclusivos.
Note
Esta propriedade não pode ser definida true para itens de menu de topo.