Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Visual Basic Drop Down Menu

0.00/5 (No votes)
17 Mar 2014 2  
Visual Basic Drop Down Menu

Introduction

Creating Visual Basic drop down menu is pretty straightforward. The main idea is to create a menu strip. Then add top level menu items and sub menus in it. After defining menus, the Menu Strip is associated with the Form to display on runtime.

I have created an example application to demonstrate Visual Basic drop down menu. Paste the following code in your WindowsApplication1:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateMenu()
    End Sub

    Public Sub CreateMenu()

        '1.Define Main Menu and top level MenuItem
        Dim menuStrip1 As New MenuStrip
        Dim toolStripFileMenuItem As New ToolStripMenuItem("File")

        '2.Define and Add sub menu items under File Menu
        Dim imageMenuItem As New ToolStripMenuItem("&Menu with Image", My.Resources.Icon)
        Dim exitMenuItem As New ToolStripMenuItem("&Exit")
        toolStripFileMenuItem.DropDownItems.Add(imageMenuItem)
        toolStripFileMenuItem.DropDownItems.Add(exitMenuItem)

        '3.Add File Menu to MenuStrip
        menuStrip1.Items.Add(toolStripFileMenuItem)

        '4.Add Menu Strip to the Form.
        Me.Controls.Add(menuStrip1)
        Me.MainMenuStrip = menuStrip1

        '5.Adding functionality to the Exit MenuItem using Click event 
        AddHandler exitMenuItem.Click, AddressOf Me.exitMenuItem_Click

    End Sub

    Private Sub exitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

End Class

Now run the application using F5. You will be happy to see the Menu Strip in action.

Visual basic drop down menu

Description of Code for Visual Basic Drop Down Menu

  • At application start, CreateMenu() method is called from Form1_Load.
  • CreateMenu() method works in five major steps. These are shown as comments and are easy to understand.
    1. First of all, we define a MenuStrip and a top level MenuItem named File.
    2. The next step is to create the sub MenuItems. We have created two items in drop down menu Menu with Image and Exit. Note that “&” before the Menu Text indicates the shortcut key for that Menu. You can set any letter as shortcut key by adding “&” before it. Later, we add sub menus to the drop down list of File MenuItem.
    3. Now add File MenuItem to MenuStrip.
    4. Add MenuStrip to the form.
    5. Add functionality to the Exit MenuItem using the AddHandler. In this way, exitMenuItem_Click sub is called when user clicks Exit menu. exitMenuItem_Click method simply closes the application and debugging stops.

Accessing Project Resources

As you have noticed, we have associated Icon image with imageMenuItem using My.Resources.Icon. If you don’t have any image available in your resources, just go to project properties. Now click resources tab and Add Resource with Existing File.

Visual basic drop down menu

Adding Tool Tip Text for Menu

Add the following piece of code in CreateMenu() right after defining the exitMenuItem (after Line 15 in the above code).

imageMenuItem.ToolTipText = "This is tool tip text for menu with an image"

Run the program to see the tool tip text.

Visual basic drop down menu

Adding Sub Menus

Adding sub Menu Items is simple and the same idea applies as described above. The following code creates a sub menu under Menu with Image:

Dim newMenuItem1 As New ToolStripMenuItem("New Sub Menu")
imageMenuItem.DropDownItems.Add(newMenuItem1)

Visual basic drop down menu

Other Tool Strip Items

ToolStripMenuItems are a great way to start with. You can also define ToolStripComboBox and ToolStripTextBox to be shown in menu strip.

  • Place a new Label1 on the Form.
  • Add the following code in CreateMenu() method:
    Dim newToolStripTextBox As New ToolStripTextBox
    toolStripFileMenuItem.DropDownItems.Add(newToolStripTextBox)
    AddHandler newToolStripTextBox.TextChanged, AddressOf Me.newToolStripTextBox_Click
  • Define a new method for handling ToolStripTextBox text changed event:
    Private Sub newToolStripTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Label1.Text = sender.ToString
        End Sub

After modifications, complete application code becomes like this:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateMenu()
    End Sub

    Public Sub CreateMenu()

        'Define Main Menu and top level MenuItem
        Dim menuStrip1 As New MenuStrip
        Dim toolStripFileMenuItem As New ToolStripMenuItem("File")

        'Define and Add sub menu items under File Menu
        Dim imageMenuItem As New ToolStripMenuItem("&Menu with Image", My.Resources.Icon)
        Dim exitMenuItem As New ToolStripMenuItem("&Exit")
        imageMenuItem.ToolTipText = "This is tool tip text for menu with an image"
        Dim newMenuItem1 As New ToolStripMenuItem("New Sub Menu")
        Dim newToolStripTextBox As New ToolStripTextBox

        imageMenuItem.DropDownItems.Add(newMenuItem1)
        toolStripFileMenuItem.DropDownItems.Add(imageMenuItem)
        toolStripFileMenuItem.DropDownItems.Add(exitMenuItem)
        toolStripFileMenuItem.DropDownItems.Add(newToolStripTextBox)

        'Add File Menu to MenuStrip
        menuStrip1.Items.Add(toolStripFileMenuItem)

        'Add Menu Strip to the Form.
        Me.Controls.Add(menuStrip1)
        Me.MainMenuStrip = menuStrip1

        ' Adding functionality to the Exit MenuItem using Click event 
        AddHandler exitMenuItem.Click, AddressOf Me.exitMenuItem_Click
        AddHandler newToolStripTextBox.TextChanged, AddressOf Me.newToolStripTextBox_Click

    End Sub

    Private Sub exitMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub newToolStripTextBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Label1.Text = sender.ToString
    End Sub

End Class

In this way, you can use the text typed by the user at runtime.

Visual basic drop down menu

The post Visual basic drop down menu appeared first on Bubble Blog.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here