110,534
社区成员
发帖
与我相关
我的任务
分享
ToolStripMenuItem ParentMenu = new ToolStripMenuItem("Parent");
ToolStripMenuItem Menu1 = new ToolStripMenuItem("Menu1");
Menu1.Click += new EventHandler(this.ToolStripMenuItem_Click); //Click事件
ParentMenu.DropDownItems.Add(Menu1);
this.ToolStripMenuItem_Click(Menu1, null)
/// <summary>
/// 菜单点击事件
/// 1.将该菜单下所有选中的子菜单改为false
/// 2.将点击的菜单check改为True
/// 3.根据ID更新下一级菜单内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem mi;
//将现有项状态改为选中项
ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
//将原来被选中的菜单项 取消选中
ToolStrip test = tsmi.GetCurrentParent();//这里获取的结果为null
foreach (object obj in tsmi.GetCurrentParent().Items)
{
if(obj.GetType() ==typeof(ToolStripMenuItem))
{
mi = (ToolStripMenuItem)obj;
if (mi.Checked)
{
mi.Checked = false;
}
}
}
tsmi.Checked = true;
}