Solved

VS 2005 Build a Menu from VB code

Posted on 2007-11-28
8
544 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
8 Comments
 
LVL 18

Accepted Solution

by:
jcoehoorn earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Where can i get a menustrip?
I could only see the menu control in my VS2005 tools
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:iyufa
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Forced accept.

Computer101
EE Admin
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now