How can I get info about which MenuItem that is clicked at runtime?

M

M Shafaat

Hi,
I want to develop an application with menus. I want to make only one event
handler method which handles a whole group of menu items by firstly
identifying which MenuItem sends the click event and then performing the
appropriate action. But I don't find any way to retrieve any info telling me
which specific MenuItem sends a click event at the moment. The sender and e
parameters seem to carry some very general information, not from a specific
MenuItem sending the click event.


This info must exist somewhere, because Windows knows which menu shall be
performed at the moment., but C# seems to hide it for the programmer!


Is there a way to get that info?


Regards,
M Shafaat
 
J

Jeff Louie

The standard way to respond to a menu is to register a separate event
handler for each menu item, eliminating any switches, such that there is
an event handler for each menu item. If you are dynamically adding menu
items at runtime then this approach does not work. If you must add menu
items dynamically at runtime you can declare a single event handler for
all menu items in a menu so that each menu item in a given menu calls
the same event handler.

IEnumerator enumerator= collection.GetEnumerator();
while (enumerator.MoveNext())
{
String menuName= (string)enumerator.Current;
menuItemShape.MenuItems.Add(menuName,
new EventHandler(this.shape_Click));
}

You can then act on the text of the menuitem at runtime in the single
event handler.

private void shape_Click(object sender, System.EventArgs e)
{
currentShape= ((MenuItem)sender).Text;
}

Regards,
Jeff
 

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

Top