Solved

How to enumerate a submenu collection

Posted on 2006-07-13
6
393 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 62

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 62

Expert Comment

by:Fernando Soto
ID: 17102880
What version of the Framework are you using?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:CSUGDEN
ID: 17103177
3.0
0
 
LVL 62

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 62

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.Net - 2005 Project Not Responding 24 47
Finding if Numbers are ranges 8 30
GridDataView 6 44
Set form below another form 3 25
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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now