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.

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

public bool Checked { get; set; }
/** @property */
public boolean get_Checked ()

/** @property */
public void set_Checked (boolean value)

public function get Checked () : boolean

public function set Checked (value : boolean)

Not applicable.

Property Value

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

Exception typeCondition

ArgumentException

The MenuItem is a top-level menu or has children.

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.

NoteNote:

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

The following code 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 a 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 requires that the System.Drawing namespace has been added to the form that this code is placed in. The example also requires that a TextBox has been added to the form that this example code is located in that is named textBox1.

// 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 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 

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft