Dynamic event handlers

E

eaguilar

Hi,

I am trying to dynamically generate a menu, based on entries on a text or
xml file. The text file contains the "tree" after which the menu will need to
be created. Something like the following:

Level 1
-- Level 2
-- Level 2
Level 1
-- Level 2
---- Level 3


I can read the file and generate each menu item definition, with the
corresponding "nesting", without a problem.

What I'm having difficulties with is deciding on how to handle the events
for each of the menu items. Generating the full menu dynamically would mean
having to create the event handlers dynamically, unless I have a fixed amount
of event handlers, which I'm not sure is the best approach. I might be wrong
here...

1. What's the best way to generate event handlers dynamically? I've read
about runtime IL generation, which makes sense but is a bit complex.

2. Using regular event handlers (hard coded), can I capture which of the
menu items is being clicked and what component is the menu being displayed
on? i.e., if the menu is being displayed after a right click on a button or a
picture box, can I: 1) know if it's the picture box or the button displaying
the menu? and 2) can I know what menu/sub menu is being clicked?

Any ideas will be greatly appreciated.
Thanks,

</edwin>
 
M

Morten Wennevik [C# MVP]

eaguilar said:
Hi,

I am trying to dynamically generate a menu, based on entries on a text or
xml file. The text file contains the "tree" after which the menu will need to
be created. Something like the following:

Level 1
-- Level 2
-- Level 2
Level 1
-- Level 2
---- Level 3


I can read the file and generate each menu item definition, with the
corresponding "nesting", without a problem.

What I'm having difficulties with is deciding on how to handle the events
for each of the menu items. Generating the full menu dynamically would mean
having to create the event handlers dynamically, unless I have a fixed amount
of event handlers, which I'm not sure is the best approach. I might be wrong
here...

1. What's the best way to generate event handlers dynamically? I've read
about runtime IL generation, which makes sense but is a bit complex.

That would be overkill. Having a single event handle each of the menus
should be fine. You do need an EventHandler for each of the menu items, but
that is just how things work.

foreach(Menu m in listOfMenus)
m.Click += new EventHandler(menuClick);
2. Using regular event handlers (hard coded), can I capture which of the
menu items is being clicked and what component is the menu being displayed
on? i.e., if the menu is being displayed after a right click on a button or a
picture box, can I: 1) know if it's the picture box or the button displaying
the menu? and 2) can I know what menu/sub menu is being clicked?

Well, the sender parameter in the event will have a reference to the
spesific menu item being clicked. Using the Tag property of the menu item,
you could store information to further help you identify what has to be done.
Using MenuItem.GetMainMenu/GetContextMenu would let you identify where the
MenuItem belongs. Using those would let you should be able to find the
parent menuitem etc (not sure why you would need to).
 

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