How to identify control which was _right_ clicked?

  • Thread starter Thread starter Helmut Giese
  • Start date Start date
H

Helmut Giese

Hello out there,
I have a context menu where some entries will have to be changed at
runtime depending on which control was clicked.

I install an event handler
ContextMenuStrip.Opening += new CancelEventHandler(cms_Opening);
(based on an example I found somewhere).

Now the signature of 'CancelEventHandler' is
void cms_Opening(object sender, CancelEventArgs e)
and 'CancelEventArgs' does not contain X and Y coordinates.

Currently I have no idea how to solve this - any advice will be
greatly appreciated.
Best regards
Helmut Giese
 
Hello

The control that has been clicked is identified by the object "sender"
in your method cms_opening.
Analyze this object, convert it to the proper type and you have your
control.

BR

Eric
 
I have a context menu where some entries will have to be changed at
runtime depending on which control was clicked.

I install an event handler
ContextMenuStrip.Opening += new CancelEventHandler(cms_Opening);
(based on an example I found somewhere).

Assuming you're using the ContextMenuStrip and not the older ContextMenu
class, you can do something like this:

private void cms_Opening(object sender, CancelEventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
Control source = ((ContextMenuStrip)item.Owner).SourceControl;
// And now you make decisions based on what source refers to
}

BE WARNED! There is a bug in the ContextMenuStrip which causes it to always
return null for the SourceControl property if the menu item is on a
sub-menu. Generally, context menus should not have sub-menus, but there are
times when it might be warranted.
 
Hi Jeff,
Assuming you're using the ContextMenuStrip and not the older ContextMenu
class, you can do something like this:

private void cms_Opening(object sender, CancelEventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
Control source = ((ContextMenuStrip)item.Owner).SourceControl;
// And now you make decisions based on what source refers to
}
hm, not quite:
1) 'sender' is a ContextMenuStrip and does not have an 'Owner'
2) However, it does have a SourceControl - but unfortunately it points
to the whole _Form_.

Maybe I misunderstood you?
Thanks and best regards
Helmut Giese
 
Hi Eric,
thanks but this doesn't seem to be quite right:
The control that has been clicked is identified by the object "sender"
in your method cms_opening.
Analyze this object, convert it to the proper type and you have your
control.
The 'sender' is a ContextMenuStrip and I have yet to find a way to
identify the underlying control.
Any other idea?
Thanks and best regards
Helmut Giese
 
To close this issue: It turned out that I had assigned my
ContextMenuStrip to the Form - so it is not too surprising that the
menu reports the Form as 'SourceControl'.

Now I assign to each control the context menu it should get (if at
all) - and (would you believe it) when clicked the menu can discover
its 'SourceControl'.

Now off to the next challenge and happy coding to everybody.
Helmut Giese
 
Helmut said:
[...]
Now I assign to each control the context menu it should get (if at
all) - and (would you believe it) when clicked the menu can discover
its 'SourceControl'.

I was gonna say…

Glad you worked it out. :)
 
Hi Helmut,

Sorry I answered a little bit too fast and forgot the object sender
would be a ContextMenuStrip.

Eric.
 
Hi Helmut,
Sorry I answered a little bit too fast and forgot the object sender
would be a ContextMenuStrip.
No problem. See my 'reply to self' about SourceControl and how I
managed to confude myself.
Best regards
Helmut Giese
 
private void cms_Opening(object sender, CancelEventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
Control source = ((ContextMenuStrip)item.Owner).SourceControl;
// And now you make decisions based on what source refers to
}

Argh. For posterity, I have just realized that I took this code from a Click
event where it would be raised by a menu item. The Opening event, however,
is raised by the ContextMenuStrip itself, so there is no need to do all the
casting to ToolStripMenuItem and calling the Owner property to get the
ContextMenuStrip; sender IS the ContextMenuStrip.
 
Back
Top