How to check a subitem in ContextMenuStrip

1.7k views Asked by At
(cms.Items[6] as ToolStripMenuItem).DropDownItems.Add("Close program",null, new EventHandler(CloseProgram_Click));
(cms.Items[6] as ToolStripMenuItem).DropDownItems.Add("Show message", null, new EventHandler(ShowMessage_Click));
(cms.Items[6] as ToolStripMenuItem).DropDownItems.Add("-");
(cms.Items[6] as ToolStripMenuItem).DropDownItems.Add("Do nothing", null, new EventHandler(DoNothing_Click));

private void CloseProgram_Click(object sender, EventArgs e)
{
    // I want mark this menuItem on dropdownItem
}

When i choose menuItem, I want it be checked.

Like this.

Like this.

1

There are 1 answers

4
kennyzx On

Cast the parameter sender to ToolStripMenuItem, and then you can set its Checked property.

private void CloseProgram_Click(object sender, EventArgs e)
{
    // I want mark this menuItem on dropdownItem
    ToolStripMenuItem menuitem = sender as ToolStripMenuItem;
        if (menuitem != null)
            menuitem.Checked = true;
} 

you can get the Checked state of each menu item like this

bool ischecked = (programToolStripMenuItem.DropDownItems[0] as ToolStripMenuItem).Checked;

Passing the correct index (0 for example), to get the menu item you want.