mainmenu iteration to a listbox

S

Scott

ok, I need to add to a list box each of the items that
are in my mainmenu dynamicaly at run time. Due to the
structure of adding menu items to menu items I don't see
how to do this without directly referencing each menu
item. Threfore whenever I ad a sub menu I will then have
to edit my code again for the new sub menu. I need to
avoid code changes in the future. Can anyone help iterate
through completely dynamically?

Scott
 
F

Fergus Cooney

Hi Scott,

You've got a Form with a Menu set up I presume.

Look at the code in the "Windows Form Designer generated code" region.

You'll see that every MenuItem which has others below it has them added to
the MenuItems collection of <that> MenuItem.

Eg,

Me.MenuItemHelp.Index = 2
Me.MenuItemHelp.MenuItems.AddRange _
(New MenuItem() {Me.MenuItemHelpAbout})
Me.MenuItem1HelpText = "Help"

Me.MenuItemAbout.Index = 0
Me.MenuItemAbout.Text = "About"

So a recursive tree walker starting with the MainMenu Control would be the
kind of thing you're after.

Public Sub GetMenuItemTree (oMenu As Menu, lstMenuItems As ListBox,
sPath As String)
sPath = sPath & "\"
Dim oMenuItem As MenuItem
For Each oMenuItem in oMenu.MenuItems
* lstMenuItems.Items.Add (sPath & oMenuItem.Text)
GetMenuItemTree (oMenuItem, lstMenuItems, sPath &
oMenuItem.Text)
Next
End Sub

Call it with your MainMenu.

Public Sub GetMenuItemTree (MainMenu1, lstMenuItems, "")

Note that the mainMenu does not have a Text property itself. This is why
the adding to the ListBox is done inside the loop. [A minor departure from
normal tree walking behaviour.]

If you don't want the full path, simply remove all instances of sPath.

Regards,
Fergus
 
S

Scott

Thank you Fergus, that was extremely helpful. For some
reason it had not occured to me to pass the names of the
objects and make referencing them possible. Thanks again.

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top