Click here to Skip to main content
Click here to Skip to main content

Tagged as

Visual Basic Drop Down Menu

, 17 Mar 2014
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sharjeel_Ahmad
Engineer http://bubbleblog.net/
Pakistan Pakistan
A Telecom Engineer, Visual Basic enthusiast and Android news follower. He is contributing to an online blog as content writer and administrator
You can visit him here: http://bubbleblog.net/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 17 Mar 2014
Article Copyright 2014 by Sharjeel_Ahmad
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid