Export (0) Print
Expand All

MenuItem.Checked Property

Gets or sets a value indicating whether a check mark appears next to the text of the menu item.

[Visual Basic]
Public Property Checked As Boolean
[C#]
public bool Checked {get; set;}
[C++]
public: __property bool get_Checked();
public: __property void set_Checked(bool);
[JScript]
public function get Checked() : Boolean;
public function set Checked(Boolean);

Property Value

true if the menu item to place a check mark next to the menu item; otherwise, false. The default is false.

Remarks

You can use the Checked property in combination with other menu items in a menu to provide state for an application. For example, you can place a check mark on a menu item in a group of items to identify the size of the font to be displayed for the text in an application. You can also use the Checked property to identify the selected menu item in a group of mutually exclusive menu items.

Note   This property cannot be set to true for top-level menu items.

Example

[Visual Basic, C#, C++] The following example uses the Checked property to provide the state in an application. In this example, a group of menu items are used to specify the color for the text in an TextBox control. The event handler provided is used by the Click event of three menu items. Each menu item specifies a text color, menuItemRed (Red), menuItemGreen (Green), or menuItemBlue (Blue). The event handler determines which menu item was clicked, places a checkmark to the selected menu item, and changes the text color of the form's TextBox control. The example assumes that the System.Drawing namespace has been added to the form that this code is placed in. The example also assumes that a TextBox has been added to the form that this example code is located in that is named textBox1.

[Visual Basic] 
' 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


[C#] 
// 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;
    }
 }


[C++] 
// 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;
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

MenuItem Class | MenuItem Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft