2

I have a ComboBox in a Winforms C# project. When the user right clicks the ComboBox to bring up a context menu, then selects an item (left click) from the default ContextMenuStrip, I want to be able to capture that event. Which event should I handle?

Is it possible to do this without making my own custom ContextMenuStrip? If I have to make my own, is there a good way to use the Windows default ContextMenuStrip as a starting point?

Edit: This question is similar: Add item to the default TextBox context menu

But it is talking about adding items to the default menu. I am asking about capturing events selecting an item from the default context menu. If I need to make a custom ContextMenuStrip to do this, that is fine, please answer that way.

Community
  • 1
  • 1
ford
  • 1,839
  • 3
  • 20
  • 38

2 Answers2

0

Sorry, I had initially misunderstood the question, but have now modified my answer to show you how to detect a click from the context menu

While searching for a solution, I came across many articles that pointed to WinProc. Going down that avenue, I came across the following

Since you are interested in the click command, I went into Menu Notifications and then checked out the WM_MENUCOMMAND message.

You will then have to create a user control and change it to inherit from textbox and add the following overrides

public partial class TextBoxUsingDefaultContextMenu : TextBox
{
    public TextBoxUsingDefaultContextMenu()
    {
        InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
        const int WM_CONTEXTMENU = 0x007B;
        const int WM_MENUCOMMAND = 0x0126;
        const int WM_COMMAND = 0x0111;

        switch (m.Msg)
        {
            case WM_CONTEXTMENU:
                MessageBox.Show("Opening Context Menu");
                break;

            case WM_MENUCOMMAND:
                MessageBox.Show("WM Menu Command Event fired");
                break;

            case WM_COMMAND:
                MessageBox.Show("WM Command Event fired");
                break;

        }

        base.WndProc(ref m);
    }

    protected override void DefWndProc(ref Message m)
    {
        base.DefWndProc(ref m);
    }
}

W.r.t the code above, I can detect the "Context Menu Opening" event but not yet the clicked event. Any help here will be appreciated from others and even this topic is new to me.

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • I agree, I would follow this method for developing a custom ContextMenuStrip. However, I see nothing about detecting events from the default Windows ContextMenuStrip – ford Apr 18 '13 at 18:31
  • Thank you for updating your answer. I'll check this solution out. – ford Apr 22 '13 at 15:23
  • Hello @ford, while i could detect the WM_CONTEXTMENU event. I could not detect WM_COMMAND or WM_MENUCOMMAND. If you could get it to work, could you please inform me. Would appreciate it! – Patrick D'Souza Apr 22 '13 at 15:40
  • Romulus, thanks for your help. Looks like my answer for now is "it's far easier to make your own custom context menu". – ford Jun 10 '13 at 17:53
0

There are a couple of ContextMenuStrip events that might help you. The MouseClick event gives you a MouseEventArgs parameter that includes which mouse button was clicked. The ItemClicked event gives you a ToolStripItemClickedEventArgs parameter that includes the menu item that was clicked. MouseClick fires before ItemClicked and you'll need to use variables defined at the form class level to communicate between the two.

bdmilum
  • 1
  • 2