日期:2014-05-17 浏览次数:21165 次
private void Form1_Load(object sender, EventArgs e)
{
foreach (ToolStripMenuItem mnu in contextMenuStrip1.Items)
{
foreach(ToolStripMenuItem mnuSub in mnu.DropDownItems)
mnuSub.Click += mnu_Click;
}
contextMenuStrip1.AutoClose = false;
}
void mnu_Click(object sender, EventArgs e)
{
ToolStripMenuItem menu = sender as ToolStripMenuItem;
menu.Checked = !menu.Checked;
int checkedCount = 0;
foreach (ToolStripMenuItem mnu in (menu.OwnerItem as ToolStripMenuItem).DropDownItems)
{
if (mnu.Checked)
checkedCount++;
}
if (checkedCount < 5) //这里举例说CheckBox小于5个,不关闭contextMenuStrip1,你按你实际需要的数量修改
(menu.OwnerItem as ToolStripMenuItem).ShowDropDown();
}