473,394 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

mainmenu iteration to a listbox

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
Nov 20 '05 #1
2 1582
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
Nov 20 '05 #2
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

-----Original Message-----
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 tothe 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 thekind 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 whythe adding to the ListBox is done inside the loop. [A minor departure fromnormal tree walking behaviour.]

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

Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anand Ganesh | last post by:
Hi All, I am trying to create a control - .dll file. In the toolbox the MainMenu tool is disabled. What should I do to invoke it? Why is it disbaled?
2
by: Jim Heavey | last post by:
Say I have a listbox and I need to go through each entry in the same order that the are identified in the listbox. I know if I create my own counter an manually update the counter as I process...
2
by: Bob | last post by:
Hi, I have a list of widgets. I want to iterate through the selected items collection and modify one of the widgets properties. I tried foreach(widget w in lst.SelectedItems) w.MyProperty =...
5
by: Mitchell Vincent | last post by:
A picture says a thousand words, so here are 2! Design Time: http://www.ksoftware.net/examples/Clip1.jpg Run Time: http://www.ksoftware.net/examples/Clip2.jpg The problems started when I...
2
by: polocar | last post by:
Hi, I'm writing a program using Visual C# 2005 Professional Edition, and I was trying to assign multiple MainMenu objects (one by one, of course) to the same Form (let's suppose 2 MainMenu...
0
by: genojoe | last post by:
Included below are four methods and a Friend that can be inserted directly into a form that already contains Button1, Button2, and MenuStrip1. Button1 populates the MainMenu control, button2...
1
by: Benson | last post by:
I have a MainMenu (MenuStrip) which will be loaded (dynamic by codes) after the program Login form. The first time run is ok. But when I close the MainMenu and go back to Login form, and then go to...
2
by: Academia | last post by:
In an old project I still use MainMenu and ContexMenu. How can I get them to appear in my Toolbox? Thanks
10
by: Adam Schaible | last post by:
Hello! My project has the following interface: (screenshot works the best) http://www.screencast.com/users/aschaible/folders/Jing/media/65d7db9d-2499-48b2-84e9-4a7af883e3c9 Device groups...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.