?
Solved

VS 2005 Build a Menu from VB code

Posted on 2007-11-28
8
Medium Priority
?
596 Views
Last Modified: 2013-11-26
I need an example for VB.net in VS2005. On how to create a menu on a fly.

Menu                              Structure
    OP1                                      GMM
    OP2                                                Inq
                                                          main
                                                 DMM
                                                           Inq
                                                          main
I was requested to do it on the fly not at the design mode.
Thank you. I am a beginner    
0
Comment
Question by:iyufa
[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
8 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 1000 total points
ID: 20369560
You'll want to create a stub for your menu in the designer.  To do that, add a MenuStrip to your form.  By default it will be called MenuStrip1.  Now let's add an a File menu:

  MenuStrip1.Items.Add(New ToolStripMenuItem("&File"))

The & tells windows how to use shortcut keys, so that Alt-F will work.  Now let's add an "Exit" option to that.  First, though, we'll need the Exit method:

    Private Sub QuitProgram(ByVal sender As Object, ByVal e As System.EventArgs)
            Environment.Exit(0)
    End Sub

The extra parameters are so the method will work with the menu.  Now we can add the new item:

    Dim menu As ToolStripMenuItem = DirectCast(MenuStrip1.Items(0), ToolStripMenuItem)
    menu.DropDownItems.Add("E&xit", Nothing, AddressOf QuitProgram)

What's going on here is that the MenuStrip's Items collection holds the base type ToolStripItem.  We need to use it as a ToolStripMenuItem, and we do that by casting it with a call to DirectCast.  Since that was it really is, this is okay.  If you will be mixing submenus with other types this is dangerous- make sure it really is a menu first.  After that we can add the new item.  I gave it the name "Exit", no image, and told it to run our QuitProgram() method when it's clicked.

In summary:
  *  Use a menustrip as a stub.
  *  Create your click methods in advance
  *  There are two basic types of items you'll keep on the menu: other menus and regular items.  It's important to keep straight which is which.
  *  Top level items are in the .Items() collection, items nested further down are in the .DropDownItems() collection.
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20369579
sorry for the delay but I have lots of work today

        Dim mnu As New MenuStrip
        Dim tsmnu As New ToolStripMenuItem("MainMenu")
        tsmnu.DropDownItems.Add("Level1", Nothing, New EventHandler(AddressOf action))
        tsmnu.DropDownItems.Add("Level2", Nothing, New EventHandler(AddressOf action))
        mnu.Items.Add(tsmnu)
        Me.MainMenuStrip = mnu
        Me.Controls.Add(mnu)

       Sub action(ByVal sender As Object, ByVal e As System.EventArgs)
          Dim tsmnu As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
          MsgBox(tsmnu.Text)
      End Sub


0
 

Author Comment

by:iyufa
ID: 20373461
Where can i get a menustrip?
I could only see the menu control in my VS2005 tools
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:iyufa
ID: 20373625
Once again I am jsut a begginer: I am not sure ai undesetnt the menustrip

Will something like this work:

  Dim itmHome As New MenuItem
        Dim itmMerchandise As New MenuItem
        Dim itmMerchantMgmt As New MenuItem

        itmHome.Text = "Home"
        itmMerchandise.Text = "Merchandise"
        itmMerchantMgmt.Text = "Merchant Mgmt"

        CSMenu.Items.Add(itmHome)
        CSMenu.Items.Add(itmMerchandise)
0
 

Author Comment

by:iyufa
ID: 20373687
This is worked. How can i add the children under each of the main header?
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 1000 total points
ID: 20409546
This is one level

        Dim tsmnu As New ToolStripMenuItem("MainMenu")
        tsmnu.DropDownItems.Add("Level1", Nothing, New EventHandler(AddressOf action))
        tsmnu.DropDownItems.Add("Level2", Nothing, New EventHandler(AddressOf action))
        mnu.Items.Add(tsmnu)

But you can do more levels like this:

        Dim tsmnu As New ToolStripMenuItem("MainMenu1")
        tsmnu.DropDownItems.Add("Level1", Nothing, New EventHandler(AddressOf action))
        tsmnu.DropDownItems.Add("Level2", Nothing, New EventHandler(AddressOf action))
        mnu.Items.Add(tsmnu)
        Dim tsmnu2 As New ToolStripMenuItem("MainMenu2")
        tsmnu2.DropDownItems.Add("Level3", Nothing, New EventHandler(AddressOf action))
        tsmnu2.DropDownItems.Add("Level4", Nothing, New EventHandler(AddressOf action))
        mnu.Items.Add(tsmnu2)
 
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21447700
Forced accept.

Computer101
EE Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project
Suggested Courses

777 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