From: Helmut Giese on
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
From: eric on
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

On 24 fév, 09:58, Helmut Giese <hgi...(a)ratiosoft.com> wrote:
> 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

From: Jeff Johnson on
"Helmut Giese" <hgiese(a)ratiosoft.com> wrote in message
news:03q9o5par39ln4akfc22ke68j1ohieefki(a)4ax.com...

> 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.


From: Helmut Giese on
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

From: Helmut Giese on
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
 |  Next  |  Last
Pages: 1 2
Prev: Help with insert using MySql
Next: NET 4.0 and NET 3.5