如何在ContextMenuStrip中检查子项

时间:2014-11-30 03:25:29

标签: c# checked contextmenustrip subitem

(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
}

当我选择menuItem时,我想要检查它。

喜欢这个。

Like this.

1 个答案:

答案 0 :(得分:0)

将参数sender投射到ToolStripMenuItem,然后您可以设置其Checked属性。

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;
} 

您可以像这样获得每个菜单项的Checked状态

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

传递正确的索引(例如0),以获取所需的菜单项。