|
-
Aug 20th, 2002, 01:48 PM
#1
Thread Starter
New Member
Finding the name of a menu item
Does anyone know how to find the NAME of an individual menu item if it is part of an ArrayList or Collection? My security method for forms in VB6 has always been to add controls to a collection when loading the form, then do a For..Each loop on the collection, checking to see if the control's name is in the user's list of permitted controls. The list of permitted controls is derived from the list of database roles that the user belongs to, either directly or indirectly. If it is not in the list, I make the control invisible.
This is a problem in .NET, because the MenuItem object doesn't have a Name property, nor does a ToolBarButton. I tried the ToString method, but the name isn't contained there either. I could possibly use the Text property, but this isn't guaranteed to be unique among all of the menu items on a form, and it can contain spaces, but database roles cannot.
It seems like I will have to set up a class that contains the control and has a Name property that I can refer to. I can do this, but it seems like I am missing something simple.
-
Aug 20th, 2002, 02:42 PM
#2
There is a FindMenuItem function that might be what you need, but I cant find any documentation on how use it
it takes 2 parameters and integer and an intptr...but again I dont know what they represent. May try playing around with it.
-
Aug 20th, 2002, 09:41 PM
#3
Member
I've had almost the same problem. I've solved it this way, hope this be useful. Try to use 'Text' property. If it's not appropriate - usually it's not because of duplicate menu item texts or localized Apps - try this:
Inherit a class from System.Windows.Forms.MenuItem and add a string 'Tag' property to that class. If you're creating your menu items in run-time set the 'Tag' property after creating each item. If you've created menu items by Windows Forms Designer, create a private sub in the form that contains menus and sets 'Tag' property for each item, like:
Private Sub mySub
myNewMenuItem.Tag = "New"
'...
End Sub
Then add a call to this sub after the call to InitializeComponent in your form's constructor. And change your loop to something like this:
Dim tmpMenuItem As myInheritedMenuItem
'myInheritedMenuItem : The class that you inherit from MenuItem class
For Each tmpMenuItem In myMainMenu.MenuItems
If tmpMenuItem.Tag = "Something" Then
'Do something
End If
Next tmpMenuItem
If you're sick of setting 'Tag' properties, create a new constructer for your inherited menu item class that accepts 'Tag' as one of it's arguments and set the tag while creating the item, like:
Public Sub New(ByVal textVal As String, ByVal tagVal As String)
MyBase.New(textVal)
Me.Tag=tagVal
End Sub
-
Aug 20th, 2002, 10:11 PM
#4
PowerPoster
Don't you just love OOP. Got to love the ease to make your own class that is inherited and add more good things to it! With things like this, why would you perfer VB6 over .Net.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|