Dynamically Adding MenuStrip and its MenuItems in VB.Net 2005 ?

L

Luqman

How can I dynamically Add menuStrip and its menuItems through code using
VB.Net 2005 ?

Best Regards,

Luqman
 
P

pvdg42

Luqman said:
How can I dynamically Add menuStrip and its menuItems through code using
VB.Net 2005 ?

Best Regards,

Luqman

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.
 
R

rowe_newsgroups

How can I dynamically Add menuStrip and its menuItems through code using
VB.Net 2005 ?

Best Regards,

Luqman

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

Thanks,

Seth Rowe
 
L

Luqman

Thanks Dear, thats exactly what I wanted, but how can I set the Tag Property
of the Items added in DropDownItems.

Say, I also want to set the innerItem.Tag=1234, innerItem.Name="myMenuItem"

item.DropDownItems.Add(innerItem)

Best Regards,

Luqman
 
R

rowe_newsgroups

Thanks Dear, thats exactly what I wanted, but how can I set the Tag Property
of the Items added in DropDownItems.

Say, I also want to set the innerItem.Tag=1234, innerItem.Name="myMenuItem"


Best Regards,

Luqman

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

Thanks,

Seth Rowe
 
D

Dean Slindee

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.

Ask a Question

Top