Solved

VS 2005 Build a Menu from VB code

Posted on 2007-11-28
8
567 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
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…

809 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