Re: menu event questions by Marcos
Marcos
Mon Mar 14 16:14:03 CST 2005
> 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.