menu event questions

  • Thread starter Thread starter Gregory Khrapunovich
  • Start date Start date
G

Gregory Khrapunovich

Hi,
I have two questions concerning menu events:
1) I want to share the same handler between three menu
items. Can I send menu event with a parameter so that
the handler would know who sent it?
2) How can I raise a menu event in my C# code? It should
duplicate already existing event that comes from the real
menu. I can of cause call the handler directly, but I
don't want to interrupt currently executing code.
Gregory Khrapunovich
 
1. The standard pattern for event handlers is as follows:

void EventHandler(object sender, EventArgs e);

The sender object contains a reference to the object that initiated the
event. You can cast this to the object type that raised the event.

2. Not quite sure here, but you might be able to use the Invoke/BeginInvoke
method of a Control to marshall an event to the UI thread.
This method is normally used to perform UI tasks from other threads.

Chris
 
1) I want to share the same handler between three menu
items. Can I send menu event with a parameter so that
the handler would know who sent it?

You can test the "sender" object, one of the two parameters to the handler,
for being equal to the different MenuItem objects in your application. If
you happen to have the MenuItem objects stored in an ArrayList, you could do
this:
foreach(object o in MenuItemsArrayList) {
if(sender.Equals((MenuItem)o) {
MessageBox("you clicked the " + ((MenuItem)o).Text + " menu item!");
}
}

Ok, it's a dumb example, but you get the idea.
2) How can I raise a menu event in my C# code? It should
duplicate already existing event that comes from the real
menu. I can of cause call the handler directly, but I
don't want to interrupt currently executing code.

If you're trying to call the handler directly from your UI thread, you don't
need to take any special precautions. You won't be interrupting currently
executing code because the single-threaded nature of your UI thread means
that the code which is calling the handler cannot itself get executed unless
there is nothing else going on, and thus, nothing to get interrupted.

If you're trying to invoke the handler from some other thread, then that's
another matter entirely. Explain in more detail what you're trying to do so
we can offer some specific guidelines.
 
Back
Top