PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 How to add sub-menu items at runtime?-VBForums
Results 1 to 8 of 8

Thread: How to add sub-menu items at runtime?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    10

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,773

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    10

    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?

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2016
    Posts
    10

    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.

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,266

    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

  6. #6
    Member
    Join Date
    Jan 2015
    Posts
    37

    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) ?

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,784

    Re: How to add sub-menu items at runtime?

    Please format code snippets for readability.
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public gFruitNames As New ArrayList
    4.     Public gFruitTypes As New ArrayList
    5.     Public Property AppleToolStripMenuItem As ToolStripMenuItem
    6.  
    7.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    8.         gFruitNames.AddRange({"Apple", "Banana", "Grape", "Orange"})
    9.  
    10.         Dim fruititem As New ToolStripMenuItem
    11.         fruititem = FruitsToolStripMenuItem
    12.  
    13.         For Each item In gFruitNames
    14.             fruititem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked)
    15.         Next
    16.  
    17.         gFruitTypes.AddRange({"Cox", "G Smith", "Delicious", "Gala"})
    18.  
    19.         Dim typeitem As New ToolStripMenuItem
    20.         typeitem = AppleToolStripMenuItem
    21.  
    22.         For Each item In gFruitNames
    23.             typeitem.DropDownItems.Add(item, Nothing, AddressOf MenuItemClicked)
    24.         Next
    25.  
    26.     End Sub
    27.  
    28.     Private Sub MenuItemClicked(ByVal sender As Object, ByVal e As System.EventArgs) 'EventArgs)
    29.         Dim itemselected As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
    30.         If itemselected IsNot Nothing Then
    31.             MsgBox("You've clicked " & itemselected.Text)
    32.         End If
    33.     End Sub
    34.  
    35. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,784

    Re: How to add sub-menu items at runtime?

    1) Is my code OK so far?
    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:
    1. Dim typeitem As New ToolStripMenuItem
    2. typeitem = AppleToolStripMenuItem
    is actually shorthand for this:
    vb.net Code:
    1. Dim typeitem As ToolStripMenuItem
    2. typeitem = New ToolStripMenuItem
    3. 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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         Dim fruitNames = {"Apple", "Banana", "Grape", "Orange"}
    5.  
    6.         'The FruitToolStripMenuItem was added in the designer.
    7.         For Each fruitName In fruitNames
    8.             FruitToolStripMenuItem.DropDownItems.Add(fruitName, Nothing, AddressOf MenuItemClicked)
    9.         Next
    10.  
    11.         'Get the Apple menu item that was just added.
    12.         Dim appleToolStripMenuItem = DirectCast(FruitToolStripMenuItem.DropDownItems(0), ToolStripMenuItem)
    13.  
    14.         Dim appleTypes = {"Cox", "G Smith", "Delicious", "Gala"}
    15.  
    16.         For Each appleType In appleTypes
    17.             appleToolStripMenuItem.DropDownItems.Add(appleType, Nothing, AddressOf MenuItemClicked)
    18.         Next
    19.     End Sub
    20.  
    21.     Private Sub MenuItemClicked(sender As Object, e As EventArgs)
    22.         MessageBox.Show(DirectCast(sender, ToolStripMenuItem).Text)
    23.     End Sub
    24.  
    25. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width