Creating A Menu in Visual Basic
Creating A Menu in Visual Basic
Creating A Menu in Visual Basic
To build a menu for use with your VB program, you use the Menu Editor, which appears as an
icon in the toolbar of the VB IDE. It is the circled item in the screen shot below:
Alternatively, you can invoke the Menu Editor from the Tools menu item as shown below:
1. Start a new VB project and invoke the Menu Editor using either method shown above (click the
Menu Editor toolbar icon or select the Menu Editor option from the Tools menu). The
Menu Editor screen appears, as shown below:
2. For "Caption", type &File (by placing the ampersand to the left of the "F", we establish "F" as
an access key for the File item it enables the user to drop down the File menu by keying
"Alt+F" on the keyboard in addition to clicking the "File" item with the mouse).
This example shows you how to create a popup menu (sometimes called a context menu or a
right-click menu).
1. Start a new VB project and place a label on the form. Name the label lblTestText. Set the
Caption to Test Text.
2. Open the Menu Editor, and create a top-level item with a Caption value of PopUpFormat and
the Name mnuPopuUpFormat. Also importantly uncheck the Visible checkbox (see the
circled item below). In order for a menu to be a pop-up menu, it must be invisible.
3. Create the following level-two menu items below the PopUpFormat top-level menu. (When
creating these level-two items, keep the Visible box checked.)
Caption Name
Bold mnuBold
Italic mnuItalic
Underline mnuUnderline
- (hyphen) mnuFormatSep
Cancel mnuCancel
5. Code the lblTestText_MouseDown event as shown below. Note that the Button parameter is
tested for vbRightButton as is conventional, we only want to pop up the menu if the user
right-clicks on the label. If the user clicks the right mouse button, the PopupMenu
statement is executed. It is this statement that makes the pop-up menu appear.
X As Single, _
Y As Single)
End If
End Su
The full syntax for the PopupMenu method, from MSDN, is:
5. Code the mnuBold_Click event as shown below. Note that the Checked property of the menu
item is used. When set to True, this causes a checkmark to appear to the left of the menu
item. The Checked property is typically used as a toggle.
If mnuBold.Checked Then
lblTestText.FontBold = False
mnuBold.Checked = False
Else
lblTestText.FontBold = True
mnuBold.Checked = True
End If
End Sub
If mnuItalic.Checked Then
lblTestText.FontItalic = False
mnuItalic.Checked = False
Else
lblTestText.FontItalic = True
mnuItalic.Checked = True
End If
End Sub
If mnuUnderline.Checked Then
lblTestText.FontUnderline = False
mnuUnderline.Checked = False
Else
lblTestText.FontUnderline = True
mnuUnderline.Checked = True
End If
End Sub
7. Run the program and check out the various options you have coded.
NOTES:
If desired, you can have both a "regular" menu and as many pop-up menus as you want on the
same form. Any top-level menu that has its Visible box checked in the Menu Editor will appear at
the top of the form in the menu bar you create. Any top-level menu that has its Visible box
unchecked in the Menu Editor will NOT appear at the top of the form in the menu bar, but can be
used as a pop-up menu invoked with the PopupMenu method.