简体   繁体   中英

Check which submenu item was clicked in context menu strip

There is a ContextMenuStrip in a grid control.

I have named it as GridContextMenu.

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

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

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.

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

You can use Tag for this purpose in case when your should localize your application. Moreover Tag is an object so you can put any tapy of data there. For example Enum type.

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

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.

Essentially, the ItemClicked event in the ContextMenuStrip control passes itself as the sender and a ToolStripItemClickedEventArgs object 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 ToolStripItemClickedEventArgs object and the clicked item resides in there as a ToolStripItem object. This can then be used to extract the name of the option to use in an if/switch statement as appropriate. See below:

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

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.

  `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;
            }
        }
    }`
private void SubmenuItem_Click(object sender, EventArgs e)
{        
    string clickedItemName=e.ClickedItem.Text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM