Enabling and Disabling Menu Items
Creating a Menubar
Choose "Menu Editor" from the Tools menu. The menubar will be created automatically for you.
Just enter the name of the first menu
you want (e.g., "File") as the Caption - that's what will appear on the menubar.
The only other thing you need to indicate is an object name, associated with the code
to be invoked when the user chooses that item.
Naming Conventions: Names of menu objects should begin with "mnu",
followed by the name of the menu (e.g., "mnuFile"). Names of menu item objects
should include all levels of cascaded menus necessary to reach the item plus the
name of the item itself (e.g., File->Format->Gif would have an object Name
of "mnuFileFormatGif"). One advantage of doing this is that the menu items will be
listed just after the menu in the Object list.
To add a second menu to the menubar, just press the Next button, signalling that you
want to move on to the next item on the same (menubar) level.
Press OK to dismiss the tool, and look at the form. A menubar has been created
with the menus you chose.
Adding Access Characters
Using the Menu Editor, specify an access key by putting an ampersand just before the
appropriate letter in the Caption (the letter "f" in File, for example). You should
always include an access key for items appearing on the menubar - and for most menu
items as well.
Creating Menu Items and Cascaded Menus
What you want to do is insert menu items just below the menu they're contained in, and
indented one level. Use the Insert key (or Next if you're at the bottom of the list) to
add new items to the list, and the up- and down-arrow keys to move elements arounds.
The right-arrow button will indent an element, making it become a item on that menu.
Cascaded menus are also handled automatically. Just create a new menu
item that is indented one level further. It will automatically be
placed in a submenu cascading from the item above it.
When you run the program, you will be able to select the menu(s) with the mouse or via the
access keys. That part is handled automatically by VB. But nothing happens when you try
to select a menu item. You will have to add code to do that.
To separate groups of menu items, add a separator bar by inserting
a new item and giving it a Caption of "-" (hyphen). You must also give separators a
name, and these must be distinct; they're typically mnuXXXSeparatorN, where XXX is the
name of the menu and N is a number if the menu has multiple separators.
Adding Code to Menu Items
This works the same way it does for other VB objects. Either click on the menu item
in the Form window, or bring up the Code window and choose the appropriate mnuXXXXX object
from the list of objects. For example, to have mnuFileExit cause the program to end,
choose the mnuFileExit object and add the following line of code:
Run the program. When you select the menu item - or hit Alt-F, X if you defined
the access keys - the program will exit.
You can also add code to the menu name itself (on the menubar), but this is not very
Adding Shortcut Keys
Using the Menu Editor, choose the item, then use the scrolled list called Shortcut to
select the appropriate shortcut. That's all you need to do.
Menus with Checkmarks
Add a checkmark alongside a menu item by going into the Menu Editor, selecting the menu
item, and clicking on the Checked box. When you dismiss the Menu Editor, you will see that
a checkmark occurs beside the menu item.
You will want to allow the user to change the checkmark at run time, by clicking on a
the menu item. Do this by changing the Checked property in the menu item's event handler
(see "Adding code to menu items"). You can set it explicitly to True or False, but it's
more common to simply toggle the value:
mnuXXXX.Checked = Not mnuXXXX.Checked
where XXXX is the name of the menu item. Note that this takes the checkmark away if it
was there, or adds it if it wasn't.
Enabling and Disabling Menu Items
To start the program off with a menu item disabled, just use the Enabled control in the
Menu Editor. To disable or enable it as the program runs, use the Enabled property:
mnuXXXX.Enabled = False