How to find a MenuItem's name

J

Jeff Mason

I have an application where I need to set certain menu items invisible based on a
user privilege.

We did a sinmlar thing in VB6 and used the menu item's tag property to assign an
identifier to each menu entry, then used that identifier as an index into a
collection populated at user login in order to determine the item's visibility.

Now, for some reason there's no tag property for .NET MenuItems, so I'm looking for
some way to accomplish what I was able to do in VB6. If I could get access to the
menu name assigned at design time that could work, but I can't see how to do that.
It's not available at run time via the menu item's type.

The sample code in help has hideous If/Then/ElseIf structures like:

If menuitem is mnuFile Then
...
elseif menuitem is mnuEdit then
...
elseif...

But there has to be a better way to uniquely identify a menuitem at run time.

I suppose I could derive a class from MenuItem and define my own tag or other
identifier property, but then I wouldn't be able to use that class in the menu
designer.

Is it possible to discover the specific instance of the menuitem via reflection
somehow?
 
A

AlexS

Hi, Jeff

You can use same approach, which worked in VB6. While MenuItem doesn't
provide Tag property originally, you can always derive your TaggedMenuItem,
which has this property, and use it instead of MenuItem

Simple sample

class TaggedMenuItem : MenuItem {
public object Tag {
get ...
set ...
}
}

HTH
Alex
 
J

Jeff Mason

Hi, Jeff

You can use same approach, which worked in VB6. While MenuItem doesn't
provide Tag property originally, you can always derive your TaggedMenuItem,
which has this property, and use it instead of MenuItem

Simple sample

class TaggedMenuItem : MenuItem {
public object Tag {
get ...
set ...
}
}

HTH
Alex

Thank you for your response. But, as I said in my original post:
I suppose I could derive a class from MenuItem and define my own tag or other
identifier property, but then I wouldn't be able to use that class in the menu
designer.

so I was aware of this technique.

This is a large project with many many menu entries yet to be developed, so the menu
designer is pretty important to me.

Is there another way, perhaps via refelction, to identify the menu items in a form?
 
S

Scott M

Could you just use some schema with the name of the menu item?

Also you could do a find and replace with your derived class name on the
menuitem. That would solve your problem with not being able to use the
menu designer.
 

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