The File menu, shown below, will have the following level-two items below it: New, Open, Save, Save As, Print, and Exit. Note that separator bars appear above the Save, Print, and Exit items.
The Help menu contains just one level-two item below it, About.
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:
To build the menu described above, perform the following steps.
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).
For "Name", type mnuFile.
Your Menu Editor screen should look like this:
Click the Next button.
3. Click the "right-arrow" button (shown circled below). A ellipsis (...) will appear as the next item in the menu list, indicating that this item is a level-two item (below "File").
For "Caption", type &New; for "Name", type mnuNew, and for "Shortcut", select Ctrl+N. By specifying a shortcut, you allow the user to access the associated menu item by pressing that key combination. So here, you are providing the user three ways of invoking the "New" function: (1) clicking File, then clicking New on the menu; (2) keying Alt+F,N (because we set up an access key for "N" by placing an ampersand to left of "N" in "New"); or (3) keying Ctrl+N. At this point, your Menu Editor screen should look like this:
Click the Next button.
4. For "Caption", type &Open; for "Name", type mnuOpen, and for "Shortcut", select Ctrl+O. Your Menu Editor screen should look like this:
Click the Next button.
5. For "Caption", type - (a hyphen), and for "Name", type mnuFileBar1. A single hyphen as the Caption for a menu item tells VB to create a separator bar at that location. Your Menu Editor screen should look like this:
Click the Next button.
6. For "Caption", type &Save; for "Name", type mnuSave, and for "Shortcut", select Ctrl+S. Your Menu Editor screen should look like this:
Click the Next button.
7. For "Caption", type Save &As ..., and for "Name", type mnuSaveAs. Your Menu Editor screen should look like this:
Click the Next button.
8. For "Caption", type -, and for "Name", type mnuFileBar2. Your Menu Editor screen should look like this:
Click the Next button.
9. For "Caption", type &Print;for "Name", type mnuPrint; and for "Shortcut", select Ctrl+P. Your Menu Editor screen should look like this:
Click the Next button.
10. For "Caption", type -; and for "Name", type mnuFileBar3. Your Menu Editor screen should look like this:
Click the Next button.
11. For "Caption", type E&xit, and for "Name", type mnuExit. Your Menu Editor screen should look like this:
Click the Next button.
12. Click the "left-arrow" button (shown circled below). The ellipsis (...) no longer appears, meaning we are back to the top-level items.
For "Caption", type &Help; and for "Name", type mnuHelp. Your Menu Editor screen should look like this:
Click the Next button.
13. Click the "right-arrow" button to create a level-two item below "Help". For "Caption", type &About; and for "Name", type mnuAbout. Your Menu Editor screen should look like this:
14. At this point, we are done creating our menu entries, so click the OK button. That will dismiss the menu editor and return focus to the VB IDE.
15. Back in the VB IDE, your form will now have a menu, based on what you have set up in the Menu Editor. If you click on a top-level menu item (File for example), the level-two menu will drop down:
16. Click on the New menu item. The code window for the mnuFileNew_Click event opens, as shown below. Note: Click is the only event that a menu item can respond to.
In thePlace mnuFileNew_Click event, place the code you want to execute when the user clicks the New menu item. Since this is just a demo, we will place a simple MsgBox statement in the event procedure:
MsgBox "Code for 'New' goes here.", vbInformation, "Menu Demo"
17. Code similar MsgBox statements for the Open, Save, Save As, and Print menu items:
Private Sub mnuFileOpen_Click()
MsgBox "Code for 'Open' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFileSave_Click()
MsgBox "Code for 'Save' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFileSaveAs_Click()
MsgBox "Code for 'Save As' goes here.", vbInformation, "Menu Demo"
End Sub
Private Sub mnuFilePrint_Click()
MsgBox "Code for 'Print' goes here.", vbInformation, "Menu Demo"
End Sub
18. For the Exit menu item Click event, code the statement Unload Me.
Private Sub mnuFileExit_Click()
Unload Me
End Sub
19. For the About menu item Click event, code as shown below:
Private Sub mnuHelpAbout_Click()
MsgBox "Menu Demo" & vbCrLf _
& "Copyright " & Chr$(169) & " 2004 thevbprogrammer.com", , _
"About"
End Sub
20. Run the program. Note how the code executes when you click on the various menu items. Also test the use of the access keys (e.g., Alt+F, N) and shortcut keys (e.g.,Ctrl-O).
21. Save the program and exit VB.
Download the project files for this example here.
EXAMPLE 2:
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 Visiblecheckbox (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
|
When done, your Menu Editor should look like this:
4. Click OK to save your changes. Note: When you return to the IDE, you will NOT see this menu on the form (remember it's a pop-up menu, and it will only be visible when invoked through code).
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.
Private Sub lblTestText_MouseDown(Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single)
If Button = vbRightButton Then
PopupMenu mnuPopUpFormat, vbPopupMenuRightButton
End If
End Sub
The full syntax for the PopupMenu method, from MSDN, is:
object.PopupMenu menuname, flags, x, y, boldcommand
The PopupMenu method syntax has these parts:
Part
|
Description
| |||||||||||||||||||||
object
|
Optional. An object expression that evaluates to an object in the Applies To list. If object is omitted, the form with the focus is assumed to be object.
| |||||||||||||||||||||
Menuname
|
Required. The name of the pop-up menu to be displayed. The specified menu must have at least one submenu.
| |||||||||||||||||||||
Flags
|
Optional. A value or constant that specifies the location and behavior of a pop-up menu, described as follows:
Note: To specify both a "location" constant and a "behavior" constant, add the two values together. For example:
PopupMenu MyMenu, vbPopupMenuRightAlign + vbPopupMenuRightButton
| |||||||||||||||||||||
X
|
Optional. Specifies the x-coordinate where the pop-up menu is displayed. If omitted, the mouse coordinate is used.
| |||||||||||||||||||||
Y
|
Optional. Specifies the y-coordinate where the pop-up menu is displayed. If omitted, the mouse coordinate is used.
| |||||||||||||||||||||
boldcommand
|
Optional. Specifies the name of a menu control in the pop-up menu to display its caption in bold text. If omitted, no controls in the pop-up menu appear in bold.
|
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.
Private Sub mnuBold_Click()
If mnuBold.Checked Then
lblTestText.FontBold = False
mnuBold.Checked = False
Else
lblTestText.FontBold = True
mnuBold.Checked = True
End If
End Sub
6. Code the mnuItalic_Click and mnuUnderline_Click events in a similar fashion as shown below.
Private Sub mnuItalic_Click()
If mnuItalic.Checked Then
lblTestText.FontItalic = False
mnuItalic.Checked = False
Else
lblTestText.FontItalic = True
mnuItalic.Checked = True
End If
End Sub
Private Sub mnuUnderline_Click()
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.
8. Save the program and exit VB.
Download the project files for this example here.