-
Dec 12th, 2016, 05:57 PM
#1
Thread Starter
New Member
How to add sub-menu items at runtime?
Two questions on how to create sub-menu items at runtime.
Here is what I've done (using VB.NET 2010 Express):
Created a new VB application. Used the MenuStrip toolbox control to add MenuStrip1 to the application. In the Form1 design window I typed a high level menu item called "Fruits".
Then in the coding area I added the following code to add dropdown items to the high level menu item called "FruitsToolStripMenuItem".
This works perfectly for adding menu items to the "Fruits" high level menu.
Question 1:
How can I add sub-menu items to these items? For example, how can I add sub-menu items "Delicious" and "Gala" to the "Apple" menu item?
Question 2:
When the user clicks on one of the sub-menu items, how can I determine which menu and sub-menu item was clicked?
Here's the code:
Public Class Form1
Public gFruitNames As New ArrayList
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
gFruitNames.AddRange({"Apple", "Banana", "Grape", "Orange"})
Dim fruititem As New ToolStripMenuItem
fruititem = FruitsToolStripMenuItem
For Each item In gFruitNames
fruititem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked)
Next
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub MenuItemClicked(ByVal sender As Object, ByVal e As System.EventArgs) 'EventArgs)
Dim itemselected As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
If itemselected IsNot Nothing Then
MsgBox("You've clicked " & itemselected.Text)
End If
End Sub
End Class
-
Dec 12th, 2016, 06:10 PM
#2
Re: How to add sub-menu items at runtime?
Q1 - the same way you did to add it to the main menu in the first place... create our list of menu items, then add them to the Apple menu item... the items of a menu aren't any different from the menu items of the main menu... it all works the same.
Q2 - when creating the items, you'll want to use AddHAndler to wire them up to an event handler for clicks.... that's the only thing you're missing.
-tg
-
Dec 12th, 2016, 06:25 PM
#3
Thread Starter
New Member
Re: How to add sub-menu items at runtime?
Sorry but I'm not sure I understand.
When I added the menu items for "Apple", "Banana", etc. I added them to the fruititem dropdown.
If I add the sub-menu items (e.g. "Delicious") to fruititem, they will just show up as main menu items.
If I understand you I would use a statement like this:
xxxxx.DropDownItems.Add("Delicious",nothing,AddressOf MenuItemClicked)
What is xxxxx?
-
Dec 12th, 2016, 08:32 PM
#4
Thread Starter
New Member
Re: How to add sub-menu items at runtime?
Sorry, I guess I should have stated that the fruit I used in my example is just for illustration purposes. The program will ultimately have menu and sub-menu items of Directory and File names. That is why I need to create the menu names at runtime.
Thanks for your help.
-
Dec 19th, 2016, 04:30 AM
#5
Re: How to add sub-menu items at runtime?
You should turn Option Strict On
Also I personally don't like array lists since they hold objects (unless of course I want a list that holds items with no common base besides object)... I would personally use a List(Of String) if it needs to be modified...
If it doesn't need to be you could use a String() array... and change gFruitNames.AddRange({"Apple", "Banana", "Grape", "Orange"}) to gFruitNames = {"Apple", "Banana", "Grape", "Orange"}
Kris
-
Feb 23rd, 2018, 11:36 AM
#6
Member
Re: How to add sub-menu items at runtime?
I want to do the same as BillMc and I'm stuck at the same point. I have added some lines to the code which now looks like this:
Public Class Form1
Public gFruitNames As New ArrayList
Public gFruitTypes As New ArrayList
Public Property AppleToolStripMenuItem As ToolStripMenuItem
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
gFruitNames.AddRange({"Apple", "Banana", "Grape", "Orange"})
Dim fruititem As New ToolStripMenuItem
fruititem = FruitsToolStripMenuItem
For Each item In gFruitNames
fruititem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked)
Next
gFruitTypes.AddRange({"Cox", "G Smith", "Delicious", "Gala"})
Dim typeitem As New ToolStripMenuItem
typeitem = AppleToolStripMenuItem
For Each item In gFruitNames
typeitem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked)
Next
End Sub
Private Sub MenuItemClicked(ByVal sender As Object, ByVal e As System.EventArgs) 'EventArgs)
Dim itemselected As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
If itemselected IsNot Nothing Then
MsgBox("You've clicked " & itemselected.Text)
End If
End Sub
End Class
Can I have some advice please
1) Is my code OK so far?
2) What is the syntax for AddHandler and where does it fit in (suggested by techgnome) ?
-
Feb 23rd, 2018, 07:56 PM
#7
Re: How to add sub-menu items at runtime?
Please format code snippets for readability.
vb.net Code:
Public Class Form1 Public gFruitNames As New ArrayList Public gFruitTypes As New ArrayList Public Property AppleToolStripMenuItem As ToolStripMenuItem Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load gFruitNames.AddRange({"Apple", "Banana", "Grape", "Orange"}) Dim fruititem As New ToolStripMenuItem fruititem = FruitsToolStripMenuItem For Each item In gFruitNames fruititem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked) Next gFruitTypes.AddRange({"Cox", "G Smith", "Delicious", "Gala"}) Dim typeitem As New ToolStripMenuItem typeitem = AppleToolStripMenuItem For Each item In gFruitNames typeitem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked) Next End Sub Private Sub MenuItemClicked(ByVal sender As Object, ByVal e As System.EventArgs) 'EventArgs) Dim itemselected As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem) If itemselected IsNot Nothing Then MsgBox("You've clicked " & itemselected.Text) End If End Sub End Class
-
Feb 23rd, 2018, 08:19 PM
#8
Re: How to add sub-menu items at runtime?
Does it work? If so then it is OK. I can see a couple of small issues though. You appear not to know what the New keyword does. Here:
Code:
Dim fruititem As New ToolStripMenuItem
fruititem = FruitsToolStripMenuItem
and here:
Code:
Dim typeitem As New ToolStripMenuItem
typeitem = AppleToolStripMenuItem
You declare a variable, create a new ToolStripMenuItem object, assign the new object to the variable, then immediately assign an existing object to the variable. What's the point in creating a new object if you don't actually want a new object? This code:
vb.net Code:
Dim typeitem As New ToolStripMenuItem typeitem = AppleToolStripMenuItem
is actually shorthand for this:
vb.net Code:
Dim typeitem As ToolStripMenuItem typeitem = New ToolStripMenuItem typeitem = AppleToolStripMenuItem
Hopefully you can see that the middle line is not only pointless, it also creates an object, thus consuming resources, that will never be used. Don't use the New keyword unless you actually want a new object.
You also seem to have an issue with AppleToolStripMenuItem. You declare that as a property but, as far as I can tell, you never assign anything to it. You add a bunch of subitems to FruitsToolStripMenuItem and one of them has the text "Apple" on it, but you never assign that item to the AppleToolStripMenuItem property. That means that later, when you try to add subitems to AppleToolStripMenuItem, you're adding those subitems to a menu item that doesn't exist.
You need to think about what objects your variables actually refer to. If you have a menu item on your form and you want to add subitems to it then you need to use a variable that actually refers to that object. It can't refer to some other object and it can't refer to no object. Here's an example of adding two sublevels to an existing menu item:
vb.net Code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim fruitNames = {"Apple", "Banana", "Grape", "Orange"} 'The FruitToolStripMenuItem was added in the designer. For Each fruitName In fruitNames FruitToolStripMenuItem.DropDownItems.Add(fruitName, Nothing, AddressOf MenuItemClicked) Next 'Get the Apple menu item that was just added. Dim appleToolStripMenuItem = DirectCast(FruitToolStripMenuItem.DropDownItems(0), ToolStripMenuItem) Dim appleTypes = {"Cox", "G Smith", "Delicious", "Gala"} For Each appleType In appleTypes appleToolStripMenuItem.DropDownItems.Add(appleType, Nothing, AddressOf MenuItemClicked) Next End Sub Private Sub MenuItemClicked(sender As Object, e As EventArgs) MessageBox.Show(DirectCast(sender, ToolStripMenuItem).Text) End Sub End Class
2) What is the syntax for AddHandler and where does it fit in (suggested by techgnome) ?
@PaMarn, you don't have to use AddHandler with a menu item because you're adding the handler here:
Code:
typeitem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked)
AddHandler is used to specify a delegate to a method that will be executed when the event is raised. That's exactly what you're doing in that code already. The AddressOf operator creates a delegate so you're specifying that the MenuItemClicked method should be executed when a menu item raises is Click event.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|