动态禁用特定上下文菜单项

时间:2013-06-28 05:05:00

标签: c# .net winforms

我在上下文菜单中添加了4个菜单。如果在单击开始上下文菜单项期间,如何禁用该特定("Start")菜单项?

ContextMenu conMenu1 = new ContextMenu();
public Form1()
{
            InitializeComponent();
    conMenu1.MenuItems.Add("Start", new System.EventHandler(this.Start_Click));
    conMenu1.MenuItems.Add("Pause", new System.EventHandler(this.Pause_Click));
    conMenu1.MenuItems.Add("Resume", new System.EventHandler(this.Resume_Click));
    conMenu1.MenuItems.Add("Stop", new System.EventHandler(this.Stop_Click));
}

private void Start_Click(object sender, EventArgs e)
{
    // Functionalities to disable start context menu item
}

2 个答案:

答案 0 :(得分:17)

你可以这样做。处理ContextMenu.Opening事件

private void conMenu1_Opening(object sender, CancelEventArgs e)
{
    conMenu1.Items[0].Enabled= false;
}

答案 1 :(得分:0)

使用PopUp事件,例如

声明

var trayMenu = new ContextMenu();
trayMenu.Popup += MenuOpening;

trayMenu.MenuItems.Add(...);
...

订阅的事件

private void MenuOpening(object sender, EventArgs e)
{         
    var cm = sender as ContextMenu;

    if (cm != null)
         cm.MenuItems[0].Enabled = false;
}