Check which submenu item was clicked in context menu strip

后端 未结 5 552
别那么骄傲
别那么骄傲 2021-01-06 05:53

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

5条回答
  •  臣服心动
    2021-01-06 06:16

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

提交回复
热议问题