Passing parameter to MenuItem.Click handler

  • Thread starter Thread starter Valerie Hough
  • Start date Start date
V

Valerie Hough

I would like to have a number of items on my C# menu and I would like them
all to invoke the same handler, but with each passing different data (say
some integer) that will be based on which menu item was clicked.

I can derive a class from EventArgs, but how do I associate a new instance
of this derived class with a particular menu item click. (the result I want
would be directly analogous to the MouseXXX events that provide dynamic
information like cursor position, etc.

I have seen a couple of discussion threads using
Delegate.CreateDelegate(), but they don't seem to do quite what I want.

Thanks in advance.
Valerie Hough

Thanks in advance.
 
I would like to have a number of items on my C# menu and I would like them
all to invoke the same handler, but with each passing different data (say
some integer) that will be based on which menu item was clicked.

You don't need to extend EventArgs to do that, just examine the
sender. It should be the MenuItem object that was clicked on.
I can derive a class from EventArgs, but how do I associate a new instance
of this derived class with a particular menu item click. (the result I want
would be directly analogous to the MouseXXX events that provide dynamic
information like cursor position, etc.

You can't do this with a standard menu item. You would have to derive
your own class and override OnClick to supply your EventArgs instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top