Results 1 to 4 of 4

Thread: Finding the name of a menu item

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Location
    Columbus, Ohio
    Posts
    4

    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.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Member Vahid's Avatar
    Join Date
    Aug 2002
    Location
    Iran
    Posts
    37
    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

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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
  •  



Click Here to Expand Forum to Full Width