C# 动态禁用特定上下文菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17357560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:16:59 来源:igfitidea点击:
Dynamically Disable Particular Context Menu Item
提问by Vignesh
I've added 4 menus in context menu. If during the start context menu item is clicked, how to disable that particular ("Start")menu item?
我在上下文菜单中添加了 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
}
采纳答案by Rajeev Kumar
You can do like this. Handle the ContextMenu.Opening Event
你可以这样做。处理 ContextMenu.Opening 事件
private void conMenu1_Opening(object sender, CancelEventArgs e)
{
conMenu1.Items[0].Enabled= false;
}
回答by ΩmegaMan
Use PopUpevent such as
使用PopUp事件,例如
Declaration
宣言
var trayMenu = new ContextMenu();
trayMenu.Popup += MenuOpening;
trayMenu.MenuItems.Add(...);
...
Subscribed Event
订阅事件
private void MenuOpening(object sender, EventArgs e)
{
var cm = sender as ContextMenu;
if (cm != null)
cm.MenuItems[0].Enabled = false;
}

