Solved

VS 2005 Build a Menu from VB code

Posted on 2007-11-28
8
574 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
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.

791 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