none
ContextMenuStrip clicked: Which item was clicked? RRS feed

  • Question

  • I'd like to find which of the menu items from a context menu was clicked in the method because the items in the list varies according to data. Is there something in the EventArgs to help?

    Thursday, August 1, 2019 8:23 PM

All replies

  • Hello,

    See the following example, does it fit your needs?

    http://sstut.com/csharpdotnet/determine-which-item-was-clicked-in-menu.php


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, August 1, 2019 9:27 PM
    Moderator
  • Hi Tom,

    To get the clicked item, you can use "e.ClickedItem.Text" in event "ItemClicked". Just like the following code.

        private void Form1_Load(object sender, EventArgs e)
        {
            this.ContextMenuStrip = contextMenuStrip1;
        }
    
        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            MessageBox.Show(e.ClickedItem.Text);
        }

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 2, 2019 1:36 AM
    Moderator
  • But if I make my click event handler:

    private void ItemPicked(object sender, ToolStripItemClickedEventArgs e)
    {
    			
    
    }

    It does not like when I put it in the menu:

    TheMenu.Items.Add(ds, Properties.Resources.calendar, ItemPicked);

    saying "Cannot convert from 'method group' to Event Handler.

    Friday, August 9, 2019 2:24 PM
  • Oh! I can do this:

    private void ItemPicked(object sender, EventArgs e)
    {
    	ToolStripItem item = (ToolStripItem)sender;
    	string s = item.Text;
    }

    Friday, August 9, 2019 2:41 PM
  • Hello, 

    In OnClick handler you have parameter - object sender - it is an instance of the item which was clicked.


    Sincerely, Highly skilled coding monkey.

    Friday, August 9, 2019 2:44 PM
  • Hi Tom,

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 12, 2019 2:53 AM
    Moderator