Solved

How to enumerate a submenu collection

Posted on 2006-07-13
6
402 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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