[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Adding ToolStripMenuItems dynamically 3 levels deep?

Posted on 2014-02-03
9
Medium Priority
?
1,384 Views
Last Modified: 2014-02-03
I am trying to dynamically create a menu structure for an Windows Application. The structure will have 3 levels. I know how to add levels 1 and 2 but not sure how to add level 3. Below is my code:

                    For Each row As DataRow In EH.DataTable.Rows
                        If Not IsDBNull(row(2)) Then
                            strFunction = row(1)
                            iMenuLevel = row(2)
                            strParent = row(3)

                            'row(0)   f_ID
                            'row(1)   functionality
                            'row(2)   menuLevel
                            'row(3)   parent

                            Select Case iMenuLevel
                                Case 1
                                    mnu.Items.Add(strFunction)
                                    mnu.Items(x).Tag = row(0)
                                Case 2
                                    For Each subItm As ToolStripMenuItem In mnu.Items
                                        If subItm.Tag = strParent Then
                                            subItm.DropDownItems.Add(strFunction)
                                            subItm.DropDownItems.Add("-")
                                        End If
                                    Next
                                Case 3
                                    For Each sub3Item As ToolStripDropDownItem In mnu.Items
                                        If sub3Item.Tag = strParent Then
                                            sub3Item.DropDownItems.Add(strFunction)
                                            sub3Item.DropDownItems.Add("-")
                                        End If
                                    Next
                            End Select
                        End If
                    Next

Open in new window


Once I get that done, how to I create a Click_Event for each menuItem?
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 

Author Comment

by:BlakeMcKenna
ID: 39830003
Does anyone have an idea on this?
0
 
LVL 13

Expert Comment

by:ktaczala
ID: 39830159
You'll need that have some sub already defined in your app.
for example:
       Private sub Item1_Click()
           'do something
       End Sub

Then after you create the menu:
        AddHandler subitem.Click, AddressOf Item1_Click

as far as adding sub menus
just dim them in advance

case 3
dim mainmenu as menustrip 'create control
dim menuitem as toolstripmenuitem ' this is what shows on the title bar
dim subitem as toolstripmenuitem ' 1st level
dim subitem1 as toolstripmenuitem ' 2nd level
dim subitem1 as toolstripmenuitem ' 3rd level

        mainmenu.Items.Add(menuitem)
        menuitem.Text = "Main Menu"
        Me.Controls.Add(mainmenu)
        For Each item As ToolStripMenuItem In mainmenu.Items
            mitem.DropDownItems.Add(subitem)
            subitem.Text = "Sub-menu"
        Next
        For Each item As ToolStripMenuItem In mainmenu.Items
            subitem.DropDownItems.Add(subsubitem)
            subsubitem.Text = "Sub-Sub-menu"
        Next
        For Each item As ToolStripMenuItem In mainmenu.Items
            subsubitem.DropDownItems.Add(subsubsubitem)
            subsubsubitem.Text = "Sub-Sub-Sub-menu"
        Next

this adds one sub menu to each menu.  you can adjust the logic from here to add more fer level
0
 
LVL 13

Expert Comment

by:ktaczala
ID: 39830162
Oops, the second 'dim subitem1' should be 'dim subitem2'
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:BlakeMcKenna
ID: 39830350
Your code doesn't work. It only works up to the 2nd level. The 3rd Level isn't working.
0
 
LVL 13

Accepted Solution

by:
ktaczala earned 2000 total points
ID: 39830446
Sorry, I typed it with out checking it.  Here's a working copy.
see screencapture for resulting menus:

        Dim mainmenu As MenuStrip = New MenuStrip 'create control
        Dim menuitem As ToolStripMenuItem = New ToolStripMenuItem ' this is what shows on the title bar
        Dim subitem As ToolStripMenuItem = New ToolStripMenuItem ' 1st level
        Dim subitem1 As ToolStripMenuItem = New ToolStripMenuItem ' 2nd level
        Dim subitem2 As ToolStripMenuItem = New ToolStripMenuItem ' 3rd level

        mainmenu.Items.Add(menuitem)
        menuitem.Text = "Main Menu"
        Me.Controls.Add(mainmenu)
        For Each item As ToolStripMenuItem In mainmenu.Items
            menuitem.DropDownItems.Add(subitem)
            subitem.Text = "Sub-menu"
        Next
        For Each item As ToolStripMenuItem In mainmenu.Items
            subitem.DropDownItems.Add(subitem1)
            subitem1.Text = "Sub-Sub-menu"
        Next
        For Each item As ToolStripMenuItem In mainmenu.Items
            subitem1.DropDownItems.Add(subitem2)
            subitem2.Text = "Sub-Sub-Sub-menu"
        Next
ScreenCapture.jpg
0
 
LVL 13

Expert Comment

by:ktaczala
ID: 39830543
Actually you don't have to do the for each loop
once you define all the toolstripmenus  you just need to add them to which ever menu you want

for example:
to add a submenu to menuitem
    menuitem.dropdownitems.add(subitem2)

this will put "Sub-Sub--Submenu" under main menu
0
 

Author Comment

by:BlakeMcKenna
ID: 39830840
For my case, I do need the For Each loop because I'm creating this Menu structure based on Database values. So the menu is built dynamically. I still can't get it to work but I think it has to do with my database design possibly.
0
 
LVL 13

Expert Comment

by:ktaczala
ID: 39830873
since the code you gave us initially doesn't give us all the facts, we need more info.
when do you create mnu? When is 'x' declared, what is the value of 'x'.  What would the actual values of row(1-3) be, for example?
0
 

Author Comment

by:BlakeMcKenna
ID: 39831129
I finally got your code working...
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question