A good way to discover the code needed to do what you want is to add a
MenuStrip and the items you want using the GUI designer, then look at the
generated code. That will be the code you need to do the same thing at run
time.
Here's some code that will create a menu with 3 main choices with 4
drop downs each.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim menu As New MenuStrip()
For i As Integer = 0 To 2
Dim item As New ToolStripMenuItem(String.Format("Menu Item
{0}", i.ToString()))
For j As Integer = 0 To 3
Dim innerItem As New
ToolStripMenuItem(String.Format("Inner Menu Item {0}", j.ToString()))
item.DropDownItems.Add(innerItem)
Next
menu.Items.Add(item)
Next
Me.Controls.Add(menu)
End Sub
You just need to modify the item in the loop it is created in.
i.e.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim menu As New MenuStrip()
For i As Integer = 0 To 2
Dim item As New ToolStripMenuItem(String.Format("Menu Item
{0}", i.ToString()))
' Modify item here
item.Name = "myMenuItem"
For j As Integer = 0 To 3
Dim innerItem As New
ToolStripMenuItem(String.Format("Inner Menu Item {0}", j.ToString()))
' Modify inner item here
innerItem.Tag = 1234
item.DropDownItems.Add(innerItem)
Next
menu.Items.Add(item)
Next
Me.Controls.Add(menu)
End Sub
OK, now that you've got the submenu items built, how do you handle the
submenu item click event, since you have no way of writing specific menu
item event handlers?
Thanks,
Dean S
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.