如何禁用菜单条子菜单

时间:2013-07-11 06:57:29

标签: c# .net winforms

我需要动态禁用菜单项的子子项。在菜单栏中如果我clcik Run At启动时,应禁用禁用启动选项如何实现此目的。我已附上快照以供参考

// Run At Startup
 private void runAtStartupToolStripMenuItem_Click(object sender, EventArgs e)
 {
   rkApp.SetValue("TimeCalculation", "\"" + Application.ExecutablePath.ToString() + "\"");
   Environment.GetFolderPath(Environment.SpecialFolder.Startup);
   menuStrip1.Items["disableStartupToolStripMenuItem"].Enabled = false;

 }

 //Disable Startup
 private void disableStartupToolStripMenuItem_Click(object sender, EventArgs e)
 {
   rkApp.DeleteValue("TimeCalculation", false);
   menuStrip1.Items["runAtStartupToolStripMenuItem"].Enabled = false;
 }

快照:

enter image description here

1 个答案:

答案 0 :(得分:0)

使用标志:

private bool flag = true;

private void runAtStartupToolStripMenuItem_Click(object sender, EventArgs e)
{
    //...
    flag = true;
}

private void disableStartupToolStripMenuItem_Click(object sender, EventArgs e)
{
    //...
    flag = false;
}

然后使用DropDownOpening事件启用/禁用项目。

private void bToolStripMenuItem_DropDownOpening(object sender, EventArgs e) {
    runAtStartupToolStripMenuItem.Enabled = !flag;
    disableStartupToolStripMenuItem.Enabled = flag;
}

如果要隐藏该项目,请使用Available

    runAtStartupToolStripMenuItem.Available= !flag;
    disableStartupToolStripMenuItem.Available= flag;