C# 检查在上下文菜单条中单击了哪个子菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13640800/
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
Check which submenu item was clicked in context menu strip
提问by Vikas Kunte
There is a ContextMenuStrip in a grid control.
网格控件中有一个 ContextMenuStrip。
I have named it as GridContextMenu.
我将其命名为 GridContextMenu。
The GridContextMenu is populated with 4 - 5 items using the following code :
GridContextMenu 使用以下代码填充了 4-5 个项目:
gridcontextMenu.Items.Add(new ToolStripMenuItem
{
Name = Plants,
Text = Plants,
Tag = Plants,
Width = 100,
Image = <image source is put here>
});
gridcontextMenu.Items.Add(new ToolStripMenuItem
{
Name = Animals,
Text = Animals,
Tag = Animals,
Width = 100,
Image = <image source is put here>
});
For the animal menu in tool strip, i added submenu in the following way
对于工具条中的动物菜单,我通过以下方式添加了子菜单
(gridcontextMenu.Items[1] as ToolStripMenuItem).DropDownItems.Add("Tiger", image_source, new EventHandler(SubmenuItem_Click));
(gridcontextMenu.Items[1] as ToolStripMenuItem).DropDownItems.Add("Lion", image_source, new EventHandler(SubmenuItem_Click));
(gridcontextMenu.Items[1] as ToolStripMenuItem).DropDownItems.Add("Elephant", image_source, new EventHandler(SubmenuItem_Click));
In the SubmenuItem_Click event handler i need to know which animal submenu was clicked.
在 SubmenuItem_Click 事件处理程序中,我需要知道点击了哪个动物子菜单。
How to achieve this ?
如何实现这一目标?
currently i have the code for event handler in the following way :
目前我有以下方式的事件处理程序代码:
private void SubmenuItem_Click(object sender, EventArgs e)
{
}
How to check condition in this event that which animal submenu was selected ? Kindly share the answer.
在这种情况下如何检查选择了哪个动物子菜单的情况?欢迎分享答案。
采纳答案by Tigran
You can do something like this:
你可以这样做:
private void SubmenuItem_Click(object sender, EventArgs e)
{
var clickedMenuItem = sender as MenuItem;
var menuText = clickedMenuItem.Text;
switch(menuText) {
case "Tiger":
break;
case "Lion":
break;
. ...
}
}
回答by Denis Kucherov
You can use Tagfor this purpose in case when your should localize your application.
Moreover Tagis an object so you can put any tapy of data there. For example Enumtype.
Tag如果您应该本地化您的应用程序,您可以用于此目的。此外Tag是一个对象,因此您可以将任何数据磁带放在那里。例如Enum类型。
private void SubmenuItem_Click(object sender, EventArgs e)
{
var clickedMenuItem = sender as MenuItem;
EnumType item = (EnumType)clickedMenuItem.Tag;
switch(item) {
case TigeItem:
break;
case LionItem:
break;
...
}
}
回答by abubakar
private void SubmenuItem_Click(object sender, EventArgs e)
{
string clickedItemName=e.ClickedItem.Text;
}
回答by Fredulom
As I found that none of the other answers worked here, I went digging and found the proper solution. This may have been applicable only in .NET Framework 4+ but here is what I found to work.
当我发现其他答案都不起作用时,我开始挖掘并找到了正确的解决方案。这可能只适用于 .NET Framework 4+,但这是我发现的工作。
Essentially, the ItemClickedevent in the ContextMenuStripcontrol passes itself as the sender and a ToolStripItemClickedEventArgsobject when the event is raised. As you can't obtain the clicked item from the ContextMenuStrip itself, the only way to obtain this information is to interrogate the ToolStripItemClickedEventArgsobject and the clicked item resides in there as a ToolStripItemobject. This can then be used to extract the name of the option to use in an if/switch statement as appropriate. See below:
本质上,ItemClicked在事件ContextMenuStrip控制本身传递发送者和一个ToolStripItemClickedEventArgs当引发事件对象。由于您无法从 ContextMenuStrip 本身获取单击的项目,因此获取此信息的唯一方法是询问ToolStripItemClickedEventArgs对象,并且单击的项目作为ToolStripItem对象驻留在其中。然后,这可用于提取要在 if/switch 语句中适当使用的选项的名称。见下文:
To configure the EventHandler:
配置事件处理器:
...
contextMenuStrip1.ItemClicked += OnContextMenuItem_Clicked;
...
To handle the event and retrieve the text of the clicked item:
要处理事件并检索单击项目的文本:
private void OnContextMenuItem_Clicked(object sender, ToolStripMenuItemClickedEventArgs e)
{
ToolStripItem clickedItem = e.ClickedItem;
string itemName = clickedItem.Text;
...
}
Hopefully this helps someone looking for this answer in future :)
希望这有助于将来寻找此答案的人:)
回答by KellBell12
This is a way to retrieve the ToolStripMenuItem's index if you have created the ContextMenuStrip Dynamically. It is really helpful with getting Enum values. My Context menu is dynamically created and filled with the Enum Names. I hope it helps someone. Sorry for the formatting still new to posting.
如果您已动态创建 ContextMenuStrip,则这是一种检索 ToolStripMenuItem 索引的方法。获取枚举值真的很有帮助。我的上下文菜单是动态创建的,并填充了枚举名称。我希望它可以帮助某人。抱歉,发布时的格式仍然是新的。
`private void DynamiallyCreatedContextMenu_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = sender as ToolStripMenuItem;
var parent = (item.Owner as ContextMenuStrip);
for (int i = 0; i < parent.Items.Count; i++)
{
if (item == parent.Items[i])
{
index = i;
break;
}
}
}`

