1

I have a dialog with an array of TextBox controls. Each of these has the ContextMenuStrip property set to point at the same context menu control "ebContextMenu". When the user right-clicks the text box, the context menu appears, and then the user selects an item. This triggers the Click event handler for the item.

My question is, in the event handler, how do I figure out which particular TextBox control was right-clicked to bring up the context menu?

Background Information:

The Click handler for the menu item starts off with:

        ToolStripMenuItem tsmi = (ToolStripMenuItem)sender;
        ToolStrip ts = tsmi.Owner;
        Control ctl = ts.Parent;

I was expecting a reference to the TextBox in the ctl variable, but it's coming back as NULL.

My original approach was to catch the Click event for the text boxes. In the event handler I checked for the right button and then set the context menu item's Tag property and triggered the context menu. Then in the Click event handler for the menu item I could check the Tag property to figure out which TextBox was clicked.

The problem was, I was not getting right-button events for these controls. A system-level context menu was popping up instead, so I switched to using the ContextMenuStrip property. This doesn't give me the chance to set the Tag property to indicate which TextBox was clicked.

Mike Fulton
  • 920
  • 11
  • 22
  • 1
    Check my answer to this similar question: [Determine what control the ContextMenuStrip was used on](http://stackoverflow.com/questions/4886327/determine-what-control-the-contextmenustrip-was-used-on) – Cody Gray - on strike Aug 10 '13 at 17:53
  • @CodyGray I had added the answer before reading that link of yours, uhm. That's actual what the OP wants and would solve his problem, my answer is something redundant. – King King Aug 10 '13 at 18:09
  • Cody that did the trick. Make it a real answer so I can pick it! :) – Mike Fulton Aug 10 '13 at 18:18
  • @MikeFulton he doesn't want, otherwise he could have added his answer. That means you should remove/delete this question. – King King Aug 11 '13 at 00:23
  • Nah, leave the question. It's perfectly fine. We have a system in place to allow questions that have an answer elsewhere to be redirected to that other question automatically. I've initiated that process by voting to close this question as a duplicate of that one. Two other people have cast votes as well. Two more votes and it happens automatically. If the answer was helpful, you can just upvote it there. No reason to duplicate content. That just makes things harder to find. Glad it worked for you. – Cody Gray - on strike Aug 11 '13 at 09:52

0 Answers0