?
Solved

How to enumerate a submenu collection

Posted on 2006-07-13
6
Medium Priority
?
414 Views
Last Modified: 2008-01-09
If I have a mainmenu list and secondary menu items within each primary menu item, I would like to enumerate through each of the sub items to examine their properties for stored data such as :

mainmenu --> mnu1   mnu2    mn3
sub items          sub1    sub1     sub1
                        sub2    sub2    
                        sub3

I thought the code to reference these items would be something like:

frmMain.controls("mnu1.sub1").tag

but that doesnt work even though frmMain.mnu1.sub1 is in the intellisense for the form object.

HOw can I address these sub items??
0
Comment
Question by:CSUGDEN
  • 4
  • 2
6 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17101267
Hi CSUGDEN;

This code sample will visit all the Mainmanue menu items.

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        For Each mItem As MenuItem In mainmenu.MenuItems
            TraverseMenu(mItem)
        Next

    End Sub

    Private Sub TraverseMenu(ByVal mItem As MenuItem)

        Console.WriteLine(mItem.Text)
        If mItem.MenuItems.Count > 0 Then
            For Each sItems As MenuItem In mItem.MenuItems
                TraverseMenu(sItems)
            Next
        End If

    End Sub


Fernando
0
 

Author Comment

by:CSUGDEN
ID: 17102392
Fernando
This is great but if I were to address just a single entry, (as in my example) how would I do so?

Charlie
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17102880
What version of the Framework are you using?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:CSUGDEN
ID: 17103177
3.0
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 17103469
Hi CSUGDEN;

Your question was, "enumerate through each of the sub items to examine their properties"

The MainMenu Object is derived from
    System.Object
        System.MarshalByRefObject
            System.ComponentModel.Component
                System.Windows.Forms.Menu

And is not derived from
    System.Object
        System.MarshalByRefObject
            System.ComponentModel.Component
                System.Windows.Forms.Control

Because the MainMenu is not derived from the base class of Control you will not find it in the Me.Controls collections of the form. So you can not execute this statement and expect it to return an object.

    frmMain.controls("mnu1.sub1").tag

Because I think you are on VB .Net 2005 which is .Net Framework 2.0 you should be able to do this.

    Dim mi() As MenuItem = Me.MainMenu.MenuItems.Find("MENU1", True)
    Where MENU1 is the MenuItem Name.

On my version of VB .Net 2005 this does not work correctly. Event though I give the menu item a Name in the property box in design mode when I run the program the name given to the menu item is an EmptyString. If I look at the property Name in debug it is empty and if I assign a name to the Name property at run time it works.

Good Luck;

Fernando
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 17103681
Hi CSUGDEN;

I tested out MenuStrip which is .Net 2.0 replacement for the MainMenu object of earlyer versions of .Net and that works fine.

        Dim ms() As ToolStripItem = Me.MenuStrip1.Items.Find("Sub2", True)

I think you should use the new technology of MenuStrip over the other.

Fernando
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question