473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to find a MenuItem's name, or, there's no more Tag property

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?

Nov 20 '05 #1
15 2310
Jeff,

The hashtable creates a table you can look up values based on a key.
The key is an object. Store the value in a hashtable with the key being the
menuitem.

Ken
--------------
"Jeff Mason" <je******@comcast.net> wrote in message
news:57********************************@4ax.com...
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?

Nov 20 '05 #2
* Jeff Mason <je******@comcast.net> scripsit:
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.


<URL:http://groups.google.de/groups?selm=u0xLOsDCDHA.2316%40TK2MSFTNGP10.phx.gb l>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
On 30 May 2004 01:32:36 +0200, hi***************@gmx.at (Herfried K. Wagner [MVP])
wrote:
<URL:http://groups.google.de/groups?selm=u0xLOsDCDHA.2316%40TK2MSFTNGP10.phx.gb l>
Thank you for your response. Your link suggests I derive a class from MenuItem to
add a property to handle this. 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.


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 to identify the menu items in a form?

Nov 20 '05 #4
* Jeff Mason <je******@comcast.net> scripsit:
<URL:http://groups.google.de/groups?selm=u0xLOsDCDHA.2316%40TK2MSFTNGP10.phx.gb l>


Thank you for your response. Your link suggests I derive a class from MenuItem to
add a property to handle this. 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.


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 to identify the menu items in a form?


Well, I don't know about an easy way to do that...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5
On Sat, 29 May 2004 19:32:52 -0400, "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote:
Jeff,

The hashtable creates a table you can look up values based on a key.
The key is an object. Store the value in a hashtable with the key being the
menuitem.

Ken


Hi Ken,

Thanks for the response.

Your suggestion implies that the association of "tag" information by menuitem can be
set up only at run time. Since I must be able to persist menu access privilege
information by user in a database, I'm not sure how your technique would apply. Each
user would have access defined for each menu entry, so I'm envisioning a database
table storing {User, <somekindofMenuIdentifier>, AccessRights}. At app
startup/logon, then, when I identify the User, I would retrieve his menu access
rights and modify the menu accordingly, i.e. set a given MenuItem visible/invisible
based on the rights. So the problem is in persisting <somekindofMenuIdentifier> and
then relating that back to the corresponding MenuItem object.

In VB6 we defined an arbitrary ID value as <somekindofMenuIdentifier> and stored it
in the menu's Tag property at design time. At run time, then, it was easy to
identify the particular menu entry by searching for this tag value.

But I see no good way to do the equivalent in .NET :-(

Is there any way to obtain the MenuItem's name by say, reflection?

Nov 20 '05 #6
On 30 May 2004 01:32:36 +0200, hi***************@gmx.at (Herfried K. Wagner [MVP])
wrote:
<URL:http://groups.google.de/groups?selm=u0xLOsDCDHA.2316%40TK2MSFTNGP10.phx.gb l>
Thank you for your response. Your link suggests I derive a class from MenuItem to
add a property to handle this. 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.


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 to identify the menu items in a form?

Nov 20 '05 #7
* Jeff Mason <je******@comcast.net> scripsit:
<URL:http://groups.google.de/groups?selm=u0xLOsDCDHA.2316%40TK2MSFTNGP10.phx.gb l>


Thank you for your response. Your link suggests I derive a class from MenuItem to
add a property to handle this. 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.


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 to identify the menu items in a form?


Well, I don't know about an easy way to do that...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8
On Sat, 29 May 2004 19:32:52 -0400, "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote:
Jeff,

The hashtable creates a table you can look up values based on a key.
The key is an object. Store the value in a hashtable with the key being the
menuitem.

Ken


Hi Ken,

Thanks for the response.

Your suggestion implies that the association of "tag" information by menuitem can be
set up only at run time. Since I must be able to persist menu access privilege
information by user in a database, I'm not sure how your technique would apply. Each
user would have access defined for each menu entry, so I'm envisioning a database
table storing {User, <somekindofMenuIdentifier>, AccessRights}. At app
startup/logon, then, when I identify the User, I would retrieve his menu access
rights and modify the menu accordingly, i.e. set a given MenuItem visible/invisible
based on the rights. So the problem is in persisting <somekindofMenuIdentifier> and
then relating that back to the corresponding MenuItem object.

In VB6 we defined an arbitrary ID value as <somekindofMenuIdentifier> and stored it
in the menu's Tag property at design time. At run time, then, it was easy to
identify the particular menu entry by searching for this tag value.

But I see no good way to do the equivalent in .NET :-(

Is there any way to obtain the MenuItem's name by say, reflection?

Nov 20 '05 #9
How about creating an extender to add an Identifier property to a menu item?
You'll get the extra property and will still be able to use standard
MenuItems.
I'll just write one now and add it to the Menus section on my website.

--
Mick Doherty
http://homepage.ntlworld.com/mdaudi100/index.html
"Jeff Mason" <je******@comcast.net> wrote in message
news:39********************************@4ax.com...
On 30 May 2004 01:32:36 +0200, hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote:
<URL:http://groups.google.de/groups?selm=...K2MSFTNGP10.ph x.gbl>
Thank you for your response. Your link suggests I derive a class from MenuItem to add a property to handle this. But, as I said in my original post:
I suppose I could derive a class from MenuItem and define my own tag or otheridentifier property, but then I wouldn't be able to use that class in the menudesigner.
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 to identify the menu items in a form?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004
Nov 20 '05 #10
On Sun, 30 May 2004 15:32:14 +0100, "Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote:
How about creating an extender to add an Identifier property to a menu item?
You'll get the extra property and will still be able to use standard
MenuItems.
I'll just write one now and add it to the Menus section on my website.


Thank you.

I tried your MenuIdentifier class, but it didn't seem to work. Perhaps I am doing
something wrong (and I certainly don't claim to understand the extenderprovider
class), but I included your class in my project, and the "Identifier" property does
not show up on a menuitem, either at run time or design time.

Is there some other plumbing I need to hook up this extender?
Nov 20 '05 #11
Well I don't know what the problem could be since it works a treat here.
Once I drop a MenuIdentifier component on my form all the Menu Items are
extended with
Identifier On MenuIdentifier1
It was very quickly thrown together and the value is initially an empty
string. I tested it by changing the value to that of the text, but you can
change it to any string value. I then tested it wth the following code:
\\\\\\\\\\\\\\\\\\\\\\
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles MyBase.Load
CheckMenuItems(Me.Menu)
End Sub

Sub CheckMenuItems(ByVal Menu_Item As Menu)
Dim s() As String = {"Cut", "Open"}
For Each ITEM As MenuItem In Menu_Item.MenuItems
If Array.BinarySearch(s, _
MenuIdentifier1.GetIdentifier(ITEM).ToString) >= 0 Then
ITEM.Enabled = False
End If
CheckMenuItems(ITEM)
Next
End Sub
/////////////////////////
This resulted in the Cut and Open menuitems being disabled.

Do you have a MenuIdentifier component sitting next to your MainMenu
component?

--
Mick Doherty
http://homepage.ntlworld.com/mdaudi100/index.html
"Jeff Mason" <je******@comcast.net> wrote in message
news:e6********************************@4ax.com...
On Sun, 30 May 2004 15:32:14 +0100, "Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote:
How about creating an extender to add an Identifier property to a menu item?You'll get the extra property and will still be able to use standard
MenuItems.
I'll just write one now and add it to the Menus section on my website.
Thank you.

I tried your MenuIdentifier class, but it didn't seem to work. Perhaps I

am doing something wrong (and I certainly don't claim to understand the extenderprovider class), but I included your class in my project, and the "Identifier" property does not show up on a menuitem, either at run time or design time.

Is there some other plumbing I need to hook up this extender?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 27/05/2004
Nov 20 '05 #12
After adding the class to your project compile and then add the
MenuIdentifier to the toolbox. You can then drop one on your form at
designtime and the extended property should show up on all menuitems.

--
Mick Doherty
http://homepage.ntlworld.com/mdaudi100/index.html
Thank you.

I tried your MenuIdentifier class, but it didn't seem to work. Perhaps I am doing something wrong (and I certainly don't claim to understand the extenderprovider class), but I included your class in my project, and the "Identifier" property does not show up on a menuitem, either at run time or design time.

Is there some other plumbing I need to hook up this extender?

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 27/05/2004
Nov 20 '05 #13
On Mon, 31 May 2004 00:08:44 +0100, "Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote:
Once I drop a MenuIdentifier component on my form all the Menu Items are
extended with
Identifier On MenuIdentifier1


That was the piece I was missing. Thank you. It indeed "works a treat".
Nov 20 '05 #14
You're welcome.

I have modified the source to give a more meaningful name. You will now get
Tag on MenuTagger1.

--
Mick Doherty
http://homepage.ntlworld.com/mdaudi100/index.html
"Jeff Mason" <je******@comcast.net> wrote in message
news:m9********************************@4ax.com...
On Mon, 31 May 2004 00:08:44 +0100, "Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote:
Once I drop a MenuIdentifier component on my form all the Menu Items are
extended with
Identifier On MenuIdentifier1


That was the piece I was missing. Thank you. It indeed "works a treat".

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004
Nov 20 '05 #15
Jeff,
This is a large project with many many menu entries yet to be developed, so the menu designer is pretty important to me.
What I normally do is derived from MenuItem as the article suggests. Then I
carefully modify the "Windows Form Designer generated code" section to
replace all "System.Windows.Forms.MenuItem" with my derived MenuItem class.
Using "Edit - Replace in Files" if needed...

I normally reserve the property extender for when I need to add a property
to a variety of classes on a form and cannot (choose not to) extend the
individual classes...

Hope this helps
Jay

"Jeff Mason" <je******@comcast.net> wrote in message
news:39********************************@4ax.com... On 30 May 2004 01:32:36 +0200, hi***************@gmx.at (Herfried K. Wagner [MVP]) wrote:
<URL:http://groups.google.de/groups?selm=...K2MSFTNGP10.ph x.gbl>
Thank you for your response. Your link suggests I derive a class from MenuItem to add a property to handle this. But, as I said in my original post:
I suppose I could derive a class from MenuItem and define my own tag or otheridentifier property, but then I wouldn't be able to use that class in the menudesigner.
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 to identify the menu items in a form?

Nov 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rakesh | last post by:
Hi, I am able to obtain a MenuItem object's Name property @ design-time, but am not able to get the same @ run- time...why? And since MenuItem doesn't inherit from Control class, it's not...
4
by: Tony | last post by:
like this, foreach (MenuItem item in this.mainMenu1.MenuItems) { //some code //I want to get the menuitem's name here! } No name property found. please help!
6
by: Claus Holm | last post by:
I'm trying to enable a menuitem in the parent form from a mdichild. Rather than making the menuitems public, I'd go for a public method in the parent form to do the change, but when I call the...
4
by: Jeff Mason | last post by:
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...
6
by: Agnes | last post by:
dim c as controls for each c in me.controls messagebox.show(c.name) next c I found that the menu time didn't show up ?? It didn't belongs to the control in the form ??? I need to...
2
by: Agnes | last post by:
I know how to get the Menuitem's TEXT . BUT I need to get the names , Does anyone know how to do ?? Thanks I try That " For Each FileMenuItem As MenuItem In Me.Menu.menuitem" before
1
by: Michael | last post by:
Hi Everyone, I seem to be having a problem accessing a menuitem on the MDIParent from a child form. From a login (frmSecurity -child form) screen, I need to hide a menuitem based on the users...
3
by: Jason Huang | last post by:
Hi, Would someone tell me how to get a MenuItem's Name in a C# Windows Form? Thanks for help. Jason
2
by: polocar | last post by:
Hi, I'm writing a program using Visual C# 2005 Professional Edition, and I was trying to assign multiple MainMenu objects (one by one, of course) to the same Form (let's suppose 2 MainMenu...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.