Iterate through ALL menu options in a MenuStrip

  • Thread starter Thread starter Allen34
  • Start date Start date
A

Allen34

Guest
I am trying to iterate through all the menu items on a MenuStrip control in VB.Net 2008. The code below (sorta) does what I want, but it only gets me through the top level menu item across the top of the form, and then through each main menu item under the top item. For example, I have a main item named Reports. Under Reports is Daily and Weekly Reports. Under Daily is Option 1, Option 2, etc. This code will iterate through Reports, Daily and Weekly, but NOT through Option 1, Option 2, etc. OK, so that it is a terrible explaination, but that's all I got. I'm guessing I need to use some recursion to iterate all the items, but it escapes me. Any ideas?


Public Sub GetMenus(ByVal objControl As Control)
Dim objMenuStrip As MenuStrip
Dim objToolStripItem As ToolStripItem

objMenuStrip = objControl
'Get the Main Menu Items
For Each objToolStripItem In objMenuStrip.Items 'Get the main menu items
MsgBox(objToolStripItem.Name)

'Get the Sub Menu Items
For Each objMenuItem As ToolStripMenuItem In objMenuStrip.Items
For intX As

Integer = 0 To objMenuItem.DropDownItems.Count - 1
MsgBox(objToolStripItem.Name)
Next
Next
Next
End Sub


Continue reading...
 
Back
Top