Is it possible to get the control that was clicked on when the ContextMeny
is used without using the Opening
event for the ContextMenu.
"Yes, but...."
I have retrieved the control that opened the context menu (a picture box in
this case) with code like this:
private void SomeContextMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
PictureBox source =
(PictureBox)((ContextMenuStrip)item.Owner).SourceControl;
[...]
}
Now here's the but: due to a bug in VS (which affects both 2005 and 2008),
if your context menu item is on a submenu (and I know it's rare for context
menus to have submenus, but it happens), then the Owner property will ALWAYS
return null. I know this because A) I have experienced it and B) I have seen
the Connect bug report which is marked "Won't fix but maybe it'll work right
in VS 2010" (or words to that effect).