?
Solved

How to enumerate a submenu collection

Posted on 2006-07-13
6
Medium Priority
?
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 63

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 63

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 63

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 63

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

801 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