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()
Dim menuStrip1 As New MenuStrip
Dim toolStripFileMenuItem As New ToolStripMenuItem("File")
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)
menuStrip1.Items.Add(toolStripFileMenuItem)
Me.Controls.Add(menuStrip1)
Me.MainMenuStrip = menuStrip1
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 from Form1_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 level MenuItem
named File
.
- 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
.
- Now add
File MenuItem
to MenuStrip
.
- Add
MenuStrip
to the form.
- 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.
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 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()
Dim menuStrip1 As New MenuStrip
Dim toolStripFileMenuItem As New ToolStripMenuItem("File")
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)
menuStrip1.Items.Add(toolStripFileMenuItem)
Me.Controls.Add(menuStrip1)
Me.MainMenuStrip = menuStrip1
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.