Solved

VS 2005 Build a Menu from VB code

Posted on 2007-11-28
8
580 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 250 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
Industry Leaders: 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!

 

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 250 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

749 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