Can you find a MenuItem by name?

A

aualias

I have a UIManager object that sets properties on various UI objects when
certain events happen in the application (my events, not standard events).
Objects can register and unregister for these events.

If I wish to enable a menu item (but do not want expose it as a property)
from the UIManager, can I somehow use the Name property to find the
MenuItem? Or is this strictly a design time property?

Right now I have a method in the form called EnableMenuItem(string
menuItemText, bool bEnable) which recursively searches through the main menu
until it finds the MenuItem whose Text property matches. Then it sets the
Enabled property of the MenuItem. This seems cumbersome.

Is there a more direct way to find a MenuItem?
I suppose that I could expose any needed MenuItems as properties and
retrieve them directly. I prefer a less specific solution.

Alfredo
 
H

Herfried K. Wagner [MVP]

aualias said:
I have a UIManager object that sets properties on various UI objects when
certain events happen in the application (my events, not standard events).
Objects can register and unregister for these events.

If I wish to enable a menu item (but do not want expose it as a property)
from the UIManager, can I somehow use the Name property to find the
MenuItem? Or is this strictly a design time property?

Menu items do not have names, only the private members holding references to
them have names. If you want to lookup a menu item by name, add a (name,
'MenuItem') pair to a 'Hashtable' and retrieve the item by its name later.
 
G

Guest

Hi,

Just to add a bit.

MenuItem does not derive from Control and hence doesn't have the Name
property. What you see in design mode what the designer specially does.

And on finding the MenuItem, adding the MenuItem instances to the Hashtable
instead of their Names might be a better idea in some scenarios - but it of
course depends on what actually you intend to achieve.

HTH,
Rakesh Rajan
 

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