Visual Basic Drop Down Menu






4.33/5 (3 votes)
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.
Description of Code for Visual Basic Drop Down Menu
- At application start,
CreateMenu()
method is called fromForm1_Load
. CreateMenu()
method works in five major steps. These are shown as comments and are easy to understand.- First of all, we define a
MenuStrip
and a top levelMenuItem
namedFile
. - 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 ofFile MenuItem
. - Now add
File MenuItem
toMenuStrip
. - Add
MenuStrip
to the form. - Add functionality to the
Exit MenuItem
using theAddHandler
. In this way,exitMenuItem_Click sub
is called when user clicksExit
menu.exitMenuItem_Click
method simply closes the application and debugging stops.
- First of all, we define a
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.
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.
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)
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 theForm
. - 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.
The post Visual basic drop down menu appeared first on Bubble Blog.