检查其子菜单项被点击右键菜单条右键菜单、其子、菜单项

2023-09-03 06:14:30 作者:国际男王

目前在网格控件的ContextMenuStrip。

There is a ContextMenuStrip in a grid control.

我已经把它命名为GridContextMenu。

I have named it as GridContextMenu.

该GridContextMenu填充了4 - 5项使用下面的code:

The GridContextMenu is populated with 4 - 5 items using the following code :

 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));

在SubmenuItem_Click事件处理程序中,我需要知道哪些动物的子菜单被点击了。

In the SubmenuItem_Click event handler i need to know which animal submenu was clicked.

如何实现这一目标?

目前我有$ C $下通过以下方式事件处理程序:

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.

推荐答案

您可以做这样的事情:

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;
         . ...
    }
}