Lesson XVI. Working with Menus
Objectives
ü To be able to create menus to enhance application’s GUI
ü To be able to use menus to trigger Event Procedures
![]()
If you have been working with the
WindowsTM operating system, you already have been exposed to menus.
When you open a file in Microsoft WordTM, insert a PowerPointTM
slide, or exit a Windows application, you use menus to select the functionality
you need. What you may not know is that the menu selections File and Exit
are independent controls that VB can create for you using the menu editor.
Menus are groups of related
commands. The concept that a menu selection is really a control is because
menus support properties and events, just like any other controls we have been
using.
Menus support only one event, the
click event (occurs when a menu item is selected).
Before we get into the menu editor, in which you can set properties of a menu control, let us look at the complete list of menu control properties.
|
Properties |
Description |
|
Caption |
The visible text you see on the
menu item |
|
Name |
The name used in code to refer
to the menu control |
|
Checked |
Determines whether a small
check mark is displayed to the left of the menu control |
|
Visible |
Determines whether a menu
control can be seen |
|
Enabled |
If False, the text is grayed
out and cannot be selected |
|
Index |
If you create a menu control
array rather than name individual menu items uniquely, this property specifies
the menu item’s subscript within the control array. |
|
Shortcut |
A key sequence that will invoke
the menu |
|
HelpContextID |
Determines which topic in the
help file will be displayed |
|
NegotiatePosition |
Works in conjunction with OLE embedment
to determine which menu controls are displayed |
|
WindowList |
Determines whether a menu
control maintains a list of the current MDI child windows |
Our task is to create the menu structure using the Menu Editor and to set the properties for each menu. Then we add code to each Click event to perform whatever function you choose in response to a user selection of the menu items.

|
Caption |
Simply use the shortest name you can. Users hate long captions because they take up to much space on the screen and reading them slows down using the menu. Also, try to use a caption that does not have the same first letter as any other menu caption. This will allow you to use the first letter of the control caption as the shortcut - it makes it much easier for the user to remember! |
|
Name |
While it can be anything, remember that the menu event will bear this name. For easy identification, the name should be prefixed with mnu e.g. mnuFile, mnuEdit. |
|
Checked |
Menu items are
either checked or not. You can check it from within the menu editor or by
using code. Generally, you’ll add checkmarks to menu options that perform on
or off actions, such as to display or not to display a Form. For example, this code will cause a menu
item to be displayed with a small checkmark to its left:
mnuFileOpen.checked
= True |
|
Visible |
If you want
to prevent a user from having access to a menu item, simply set the visible property
to FALSE. This will keep the user from even knowing that the menu item
exists.
mnuFileOpen.visible = False
|
|
Enabled |
To allow the
user to see the menu, but not to select it, set the enabled property to
False:
MnuFileOpen.enabled = False
|
|
Shortcut |
Most users
want to be able to invoke a menu item from the keyboard. This is the property
that defines the shortcut keystrokes. When a shortcut is defined, you can
invoke the menu item from the keyboard, no matter how deep in the menu
structure the item is that you are calling.
|
![]()
Let’s create a menu structure as
shown in the following Forms.


As shown
in the previous page, The File menu has 3 menu items: Open, Close,
and Exit. When we dig in the
Open submenu, we can find the items labeled Form and Project. Under the Edit menu, we have items Copy,
Paste, Search, and Replace. Follow through the following steps:
§
Caption &File
§
Name mnuFile
§
Leave
the rest of the properties as they are: Enabled and Visible
§
Caption &Open
§
Name mnuOpen
§
Caption &Form
§
Name mnuForm
§
Shortcut Ctrl+F
§
Caption &Project
§
Name mnuProject
§
Shortcut Ctrl+P
§
Caption &Close
§
Name mnuClose
§
Enabled Disable by unchecking the checkbox
§
Caption &Exit
§
Name mnuExit
§
Caption &Edit
§
Name mnuEdit
§
Caption &Copy
§
Name mnuCopy
§
Shortcut Ctrl+C
§
Caption &Paste
§
Name mnuPaste
§
Shortcut Ctrl+V
§
Caption - (one dash)
§
Name Any name. We won't be referring to this anyway.
§
Caption &Search
§
Name mnuSearch
§
Shortcut F3
§
Caption &Replace
§
Name mnuReplace
§
Shortcut F4
§
Caption Case
Sensitive
§
Name mnuCase
§
Checked check
the checkbox
You can always edit an existing
menu structure.
§
Delete an item: just select the menu item and press the Delete
button.
§
Edit the properties of a menu item: select the item to be
edited and edit the properties.
§
Modify the order of items listed in the menu editor: select
the item and press either the up (é) or down (ê) button.
You can still use the left (ç) or right (è) buttons to move items from one level to another.
§
To insert a menu item: Select the position in the menu
hierarchy where you will insert the item and press the Insert button.
Create a new project with the
following menu bar items: Lessons, and Options. The menu structure is as follows:
A.
Lessons
1.
Mathematics
a.
Addition
b.
Subtraction
2.
Science
a.
Animals
b.
Plants
B.
Options
1.
Audio On (Checked item)
2.
Input Device (Checked item)
a.
Keyboard
b.
Mouse
Note: Only one Input Device should be checked at a time. If the user selects Keyboard, then Mouse should be unchecked.
[ Home ]