Solved

How to enumerate a submenu collection

Posted on 2006-07-13
6
399 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 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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 250 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - TypeInitializer Error 25 32
Import Data from Multiple Text Files in Excel 12 64
export data table to excel in vb.net 10 37
SqlServer no dupes 25 38
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

820 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