Visual Basic Menus
14. Edit the code
You edit the code for menu items just as you do for other controlsclick the menu item in the form under design (opening the items menu if necessary). This opens the menu items event handler, like this:
http://24.19.55.56:8080/temp/ch05\163-168.html (4 of 5) [3/14/2001 1:31:08 AM]
Private Sub mnuFileNew_Click()
End Sub
Just add the code you want to execute when the user chooses this menu item to the event handler procedure:
Private Sub mnuFileNew_Click() LoadNewDoc
End Sub
And thats itnow youve added a menu system to your program.
Modifying And Deleting Menu Items
You think the program is perfect, but the users are complaining that they dont like having the Save As item in the Edit menu and want to move it to the File menu. Is that possible?
Yes, using the Menu Editor. You can rearrange, add, or remove items in your menu with the Menu Editor, so open that tool now (as shown in Figure 5.4).
Inserting Or Deleting Items In A Menu System
To add a new item to a menu, or a new menu to the menu system, select an item in the Menu Editor, and click the Insert button. This inserts a new, empty entry into the menu just before the item you selected:
File ....New ....Open ...
Edit ....Cut ....Copy ....Paste
http://24.19.55.56:8080/temp/ch05\163-168.html (5 of 5) [3/14/2001 1:31:08 AM]
Now just enter the new items Caption and Name properties, and youre all set.
To remove a menu or menu item, just select that menu or item and click the Delete button.
Rearranging Items In A Menu System
You can use the four arrow buttons in the Menu Editor to move items up and down, as well as indent or outdent (that is, remove one level of indenting) menu items.
Heres what the arrows do:
" Right arrow Indents a menu item.
" Left arrow Outdents a menu item.
" Up arrow Moves the currently selected item up one level.
" Down arrow Moves the currently selected item down one level.
For example, to move the Save As item from the Edit menu to the File menu, just select that item and keep clicking the up arrow button until the Save As item is positioned as you want it in the File menu.
Adding A Menu Separator
Menus themselves allow you ways to group commands by function (File, Edit, and so on). Often within a menu, however, it helps the user to group menu items by function (Print, Print Preview, Page Setup, and so on). You do that with menu separators.
A menu separator is a horizontal rule that really only has one purposeto divide menu items into groups (refer back to Figure 5.1). And using the Menu Editor, you can add separators to your menus.
To add a menu separator, select an item in the Menu Editor and click Insert to create a new item just before the item you selected. To make this new item a menu
separator, just give use a hyphen (-) for its Caption property. You must give all menu items a nameeven if they dont do anythingso give it a dummy Name property value as well, such as mnuSeparator.
When you run the program, youll see the menu separators in place, as in the menu in Figure 5.5. Now were adding menu item separators to our menus.
Figure 5.5 A menu with menu separators.
http://24.19.55.56:8080/temp/ch05\168-174.html (1 of 4) [3/14/2001 1:31:44 AM]
Adding Access Characters
The Testing Departments calling again: They like the menus youve added to your program, but theres the keyboard access issue. Theoretically, they say, users should be able to use the keyboard for everything.
Its time to add access characters to your program. When the user presses the Alt key and an access character, the menu item corresponding to that access character is selected. How do you associate an access character with a menu or menu item? Its easyjust place an ampersand (&) in front of the character you want to make into the access character in that menu or items caption.
For example, if you had this menu system
File
you could make a letter in all menus or menu items into access characters by placing an ampersand in front of it:
&File
Note in the previous example that we have two itemsCut and Copyin the Edit menu that begin with C. Thats a problem, because an access character must be unique at its level (where the level is the menu bar for menus and a menu for menu items). To avoid confusion (both to the user and to Visual Basic), we make o, the second letter in Copy, the access character for that item.
The result of adding access characters to your menus at design time appears in the Menu Editor in Figure 5.6. At runtime, access characters appear underlined in menus, as shown in Figure 5.7.
http://24.19.55.56:8080/temp/ch05\168-174.html (2 of 4) [3/14/2001 1:31:44 AM]
Figure 5.6 Adding access characters.
Figure 5.7 Access characters are underlined.
To use an access key, users first open the menu in which the item they want to select appears (possibly using an access key, like Alt+F for the File menu), then they press the Alt key and the access key.
Adding Shortcut Keys
One of the most powerful aspects of menus are shortcut keyssingle keys or key combinations that let the user execute a menu command immediately (without having to open the menu the command is in, as you must do with access keys). You usually use function keys (although many PCs now go up to F16, its best to limit yourself to F1 through F10) or Ctrl key combinations for shortcut keys. For example, the standard shortcut key for Select All is Ctrl+A, and entering that shortcut selects all the text in a document.
Giving a menu item a shortcut key is very easy in the Menu Editor. Just open the Menu Editor, select the item you want to give a shortcut key to (such as the File menus New item in Figure 5.8) and select the shortcut key you want to use in the Menu Editor box labeled Shortcut. (Note that to open the Menu Editor, the form youre designing must be the active window in Visual Basic, not the code window.) In Figure 5.8, we give the New item the shortcut Ctrl+N.
Figure 5.8 Setting a shortcut key.
Thats all it takesnow run the program, as shown in Figure 5.9. You can see the Ctrl+N at the right in the menu item named Newweve installed our menu shortcut.
Figure 5.9 Shortcut key in a programs menu.
Shortcut Key Standards
Windows conventions now include a set of standard shortcut keys that are supposed to apply across most Windows applications. Here are the most common shortcut keys (be very careful when using these key combinations for other purposes; your users may expect the standard response):
" Ctrl+ASelect All
http://24.19.55.56:8080/temp/ch05\168-174.html (3 of 4) [3/14/2001 1:31:44 AM]
" Ctrl+BBold
" Ctrl+CCopy
" Ctrl+FFind
" Ctrl+GGo To
" Ctrl+HReplace
" Ctrl+IItalic
" Ctrl+JJustify
" Ctrl+NNew
" Ctrl+OOpen
" Ctrl+PPrint
" Ctrl+QQuit
" Ctrl+SSave
" Ctrl+UUnderline
" Ctrl+VPaste
" Ctrl+WClose
" Ctrl+XCut
" Ctrl+ZUndo
" F1Help
Creating Submenus
The email is inand its more praise for your program, AmazingWingDings (Deluxe version). Its gratifying to read the great reviewsbut one user asks if you couldnt place the Red, Green, and Blue color selections in the Edit menu into a submenu.
What are submenus, and how can you create them?
http://24.19.55.56:8080/temp/ch05\168-174.html (4 of 4) [3/14/2001 1:31:44 AM]
What the user wants appears in Figure 5.10. As you can see in that figure, the
Colors item in the Edit menu has a small arrow at the right. This indicates that there s a submenu attached to this menu item. Selecting the menu item opens the
submenu, as also shown in Figure 5.10. As you can see, submenus appear as menus attached to menus.
Figure 5.10 A program with a submenu.
Submenus let you organize your menu system in a compact way, and adding them to a program is simple. For example, lets say you started this way, with a Red, Green, and Blue menu item in the Edit menu:
Edit ....Cut ....Copy ....Paste ....Red ....Green ....Blue
....Select All
To put those items in a submenu, we first add a name for the submenusay, Colors:
Edit ....Cut ....Copy ....Paste ....Colors ....Red ....Green ....Blue
....Select All
All thats left is to indent (using the right arrow in the Menu Editor) the items that should go into that submenu (note that they must appear just under the submenus name):
http://24.19.55.56:8080/temp/ch05\174-180.html (1 of 5) [3/14/2001 1:33:52 AM]
Edit
Thats itclose the Menu Editor.
You add code to submenu items in the same way that you add code to menu items just click them to open the corresponding event-handling function and add the code you want, as weve done here to report the users color selection:
Private Sub mnuEditColorsBlue_Click() MsgBox ("You selected Blue")
End Sub
Private Sub mnuEditColorsGreen_Click() MsgBox ("You selected Green")
End Sub
Private Sub mnuEditColorsRed_Click() MsgBox ("You selected Red")
Creating Immediate (Bang) Menus
Sometimes youll see immediate menus (also called bang menus) in menu bars.
These are special menus that dont openwhen you merely click them in the menu bar, they execute their associated command. The name of these menus is followed with an exclamation mark (!) like this: Download! When you click the Download!
item in the menu bar, the downloading process starts at once, without opening a menu at all.
Now that toolbars are so common, one sees fewer immediate menus (that is, toolbars act very much like immediate menus are supposed to work), but some programmers still use them And because theyre easy to create, well cover them here.
To create an immediate menu, just add a menu, such as Download! (dont forget to add exclamation point on the end of Download in the Caption property, but not in
http://24.19.55.56:8080/temp/ch05\174-180.html (2 of 5) [3/14/2001 1:33:52 AM]
the Name property), and dont give it any menu items. Instead, place the code you want to run in the Click event handler for the menu itself:
Private Sub mnuDownload_Click()
MsgBox ("Downloading from the Internet...") End Sub
Thats all you need. Now when the user selects the Download! menu, this code will be executed. Were about to execute the Download! immediate menu in Figure 5.11.
Note that there is no menu opening, even though the Download! item in the menu bar is selected.
Figure 5.11 Selecting an immediate menu.
Using The Visual Basic Predefined Menus
You can use the Visual Component Manager to add a predefined menu to a form (note that not all versions of Visual Basic come with the Visual Component Manager). As you can see in the Visual Component Managers Visual
Basic|Templates|Menus folder, as shown in Figure 5.12, six predefined menus are available. These menus include a File menu, an Edit menu, a Help menu, a Window menu, and so on. To add one of these menus to a form, just select the form and double-click the menu in the Visual Component Manager.
Figure 5.12 Selecting a predefined menu.
For example, we can add a predefined File menu to a form this way. The result appears in Figure 5.13.
Figure 5.13 Using a predefined menu.
Adding a predefined menu also adds code to the form. For example, heres the skeletal code thats added when you add a predefined File menu:
Private Sub mnuFileNew_Click()
MsgBox "New File Code goes here!"
End Sub
Private Sub mnuFileOpen_Click()
http://24.19.55.56:8080/temp/ch05\174-180.html (3 of 5) [3/14/2001 1:33:52 AM]
MsgBox "Open Code goes here!"
End Sub
Private Sub mnuFilePrint_Click() MsgBox "Print Code goes here!"
End Sub
Private Sub mnuFilePrintPreview_Click() MsgBox "Print Preview Code goes here!"
End Sub
Private Sub mnuFilePrintSetup_Click() MsgBox "Print Setup Code goes here!"
End Sub
Private Sub mnuFileProperties_Click() MsgBox "Properties Code goes here!"
End Sub
Private Sub mnuFileSave_Click()
MsgBox "Save File Code goes here!"
End Sub
Private Sub mnuFileSaveAll_Click() MsgBox "Save All Code goes here!"
End Sub
Private Sub mnuFileSaveAs_Click() MsgBox "Save As Code goes here!"
End Sub
Private Sub mnuFileSend_Click() MsgBox "Send Code goes here!"
End Sub
TIP: If you dont have the Visual Component Manager, you can add a form with a predefined menu to a project. Select Project|Add Form, click the Existing tab, and
http://24.19.55.56:8080/temp/ch05\174-180.html (4 of 5) [3/14/2001 1:33:52 AM]
open the Menus folder to find the possible menu forms to add to your project.
Adding A Checkmark To A Menu Item
When you want to toggle an option in a program, such as Insert mode for entering text, its easy to add or remove checkmarks in front of menu items. Displaying a checkmark gives visual feedback to the user about the toggle state of the option, and theres two ways to add checkmarks to menu items: at design time and at runtime.
Adding Checkmarks At Design Time
To add a checkmark to a menu item at design time, you simply select the Checked box in the Menu Editor, as shown in Figure 5.14, where we add a checkmark to the Edit menus Insert item.
Figure 5.14 Adding a checkmark to a menu item at design time.
Now when the Edit menu is first displayed, the Insert item will appear checked.
Adding Checkmarks At Runtime
You can also set checkmarks at runtime using a menu items Checked property. For example, heres how we toggle the Insert items checkmark each time the user
selects that item; setting Checked to True places a checkmark in front of the item, and to False removes that checkmark:
Private Sub mnuEditInsert_Click() Static blnChecked As Boolean blnChecked = Not blnChecked
mnuEditInsert.Checked = blnChecked End Sub
Running this code toggles a checkmark in front of the Insert item, as shown in Figure 5.15.
Figure 5.15 Adding a checkmark to a menu item at runtime.
http://24.19.55.56:8080/temp/ch05\174-180.html (5 of 5) [3/14/2001 1:33:52 AM]
Disabling (Graying Out) Menu Items
To indicate to the user that a menu item is not available at a particular time (such as Copy when there is no selected text), you can disable a menu item (also called graying it out). And you can do this at design time or runtime.
Disabling Menu Items At Design Time
To disable a menu item at design time, just deselect the Enabled box in the Menu Editor, as shown in Figure 5.16, where we disable the Insert menu item.
Figure 5.16 Disabling a menu item at design time.
Now when the Edit menu is first shown, the Insert item will be disabled.
Disabling Menu Items At Runtime
You can also disable (and enable) menu items at runtime using the items Enabled property. You set this property to True to enable a menu item and to False when you want to disable an item.
For example, heres how we disable the Edit menus Insert item when the user clicks it (note that in this program there is then no way for the user to enable it again):
Private Sub mnuEditInsert_Click() mnuEditInsert.Enabled = False End Sub
Figure 5.17 shows the resultweve disabled the Insert menu item.
Figure 5.17 Disabling a menu item at runtime.
Handling MDI Form And MDI Child Menus
Youve created your new program, the SuperWizardTextEditor, and made it an MDI program. But now theres a call from the Testing Departmentusers are getting
confused. Why is the Edit menu still visible when no documents are open to edit?
Can you fix this?
Yes you can. Visual Basic lets you specify two menus in an MDI program, one for the MDI form and one for the MDI child form (and more if you have several types of MDI child forms). If the MDI form has a menu and the MDI child form has no
http://24.19.55.56:8080/temp/ch05\180-185.html (1 of 4) [3/14/2001 1:34:22 AM]
menu, the MDI forms menu is active at all times.
If, on the other hand, the MDI child form has a menu, that menu takes over the MDI forms menu system any time one or more of those child forms is open. What this means in practice is that you give the MDI form a rudimentary menu system
(typically just File and Help menus) and save the full menu system (like File, Edit, View, Insert, Format, Tools, Window, Help, and so on) for the child windows to ensure the full menu system is on display only when documents are open and those menus apply.
For example, you might add just this simple menu system to the MDI form in an MDI program. Note that you should, at a minimum, give the user some way to open a new or existing document, and you should provide access to Help:
File ....New ....Open Help
....Contents
Heres an example of a full menu system you might then give to the MDI child form, which will take over the main MDI forms menu system when a child form is open:
TIP: If the user closes all documents at any time, the MDI forms menu system
http://24.19.55.56:8080/temp/ch05\180-185.html (2 of 4) [3/14/2001 1:34:22 AM]
becomes active againits only when MDI child forms are open that their menus take over the main menu system.
Adding A List Of Open Windows To An MDI Forms Window Menu
You might have noticed that Window menus in professional MDI programs include a list of open MDI child windows, and you can select which child is active by
selecting from this list. You can add that to your program by adding all the code yourself, but theres an easier wayyou can set a menus WindowList property.
Setting a menus WindowList property to True adds a list of windows to that menu, and you can set the WindowList property in the Menu Editor simply by selecting a checkbox, as shown in Figure 5.18.
Figure 5.18 Adding a window list to a Window menu.
Now when the program runs, the menu you added a window list to will indeed display a list of open windows, separated from the rest of the menu items with a menu separator, as shown in Figure 5.19.
Figure 5.19 Our window list is active.
Youve added a touch of professionalism to your program with a single mouse click.
Making Menus And Menu Items Visible Or Invisible
The Field Testing Department is on the phone again. Someone there doesnt like the look of the 30 disabled menu items in the Edit menu. You explain that those items just dont apply in most cases, so they should be disabled. The Field Testing people suggest you just remove those items from the Edit menu until they can be used.
How does that work?
Like other Visual Basic controls, menus and menu items have a Visible property, and you can set that property to True to make a menu or menu item visible, and to False to make it invisible (and so remove it from a menu bar or menu).
For example, you might have an item in the File menu: Connect to the Internet, which is inappropriate in a computer that has no way to connect to the Internet. You can make that item disappear from the File menu by setting its Visible property to False, as we do here after checking some hypothetical variable blnCanConnect :
If blnCanConnect Then
mnuFileInternet.Visible = True
http://24.19.55.56:8080/temp/ch05\180-185.html (3 of 4) [3/14/2001 1:34:22 AM]
Else
mnuFileInternet.Visible = False End If
Making menus and menu items visible or invisible is often a better alternative to displaying menus with too many disabled items (which can frustrate the user and make a program seem inaccessible).
Creating And Displaying Pop-Up Menus
Pop-up menusthose menus that appear when you right-click a formhave become very popular these days, and we can add them to Visual Basic programs.
Creating A Pop-up Menu
To create a new pop-up menu, just use the Menu Editor as shown in Figure 5.20,
To create a new pop-up menu, just use the Menu Editor as shown in Figure 5.20,