Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which control fired the context menu?

Status
Not open for further replies.

NeilTrain

Programmer
May 27, 2003
275
0
0
US
So I have a group of controls, all created dynamically and added to a panel. On creation I set each of thier context menu properies to a context menu I have created. I want the user to be able to right click each control, and select "Disable Cotrol" from the menu, at which point the control will be removed from the control collection of the panel.

I assumed incorectly that the "sender" parameter of the MenuItem_Click event was the control that was clicked, however this is not the case, it's the menuitem itself... so how do I determine what control is being clicked? Do i have to set an index in the mousedown even or something?

any help appreciated.
 
Ok so after modifying my google search a few times I came up with an aswer. Should have spent more time searching before I posted here. Anywauys, will post it here for anyone else who comes across this thread.

In the menu item click event, you can obtain the control like this:

Code:
  Control ClickedControl = ((MenuItem)sender).GetContextMenu().SourceControl;

At which point if you know the type of control you can just cast it straight to that. I have a custom control called ProductItem so I did this:
Code:
  ProductItem clickedItem = (ProductItem)((MenuItem)sender).GetContextMenu().SourceControl;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top