Вы находитесь на странице: 1из 4

Lesson XVI.

Working with Menus

Objectives
To be able to create menus to enhance applications GUI
To be able to use menus to trigger Event Procedures
Notes
If you have been working with the Windows TM operating system, you already have been exposed to
menus. When you open a file in Microsoft Word TM, 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
Caption
Name
Checked
Visible
Enabled
Index
Shortcut
HelpContextID
NegotiatePosition
WindowList

Description
The visible text you see on the menu item
The name used in code to refer to the menu control
Determines whether a small check mark is displayed
to the left of the menu control
Determines whether a menu control can be seen
If False, the text is grayed out and cannot be selected
If you create a menu control array rather than name
individual menu items uniquely, this property specifies
the menu items subscript within the control array.
A key sequence that will invoke the menu
Determines which topic in the help file will be
displayed
Works in conjunction with OLE embedment to
determine which menu controls are displayed
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.
IN FOCUS: MENU EDITOR
The following is the Menu Editor window:

As you can see, the editor has two general sections. In the top half, you set the properties we enumerated on the
previous page. In the bottom half you create the hierarchical structure of the menu (the hierarchy determines how
the menu items are organized and displayed on the Form).
Remember that menus are only associated with a Form. No other control has a menu. VB provides
the built-in ability to manage the display of all of the menu items. You only have to create the
structure and let VB handle it from that point on.
Now, let's talk about each of the properties and see if there's some guidance on what to use for the
properties.
Caption

Name

Checked

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!
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.
Menu items are either checked or not. You can
check it from within the menu editor or by using
code. Generally, youll add checkmarks to menu
options that perform on or of 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.

Lesson in Action
Lets 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:
1. On the menu bar, click Tools.
2. Click Menu Editor.
3. Let's create the File menu. Enter the following property settings:

Caption
&File

Name
mnuFile

Leave the rest of the properties as they are: Enabled and Visible
4. Click Next.
5. Since Open is a menu item under File, click the right arrow () button. This also causes all
the succeeding new menu items to fall under the Open menu.
6. For the Open menu item:

Caption
&Open

Name
mnuOpen
7. Click Next.
8. Click the right arrow () button to create a submenu.
9. For the Form submenu:

Caption
&Form

Name
mnuForm

Shortcut
Ctrl+F
10. Click Next.
11. For the Project submenu:

Caption
&Project

Name
mnuProject

Shortcut
Ctrl+P
12. Click Next.
13. Click the left arrow () button.
14. For the Close submenu:

Caption
&Close

Name
mnuClose

Enabled
Disable by unchecking the checkbox
15. Click Next.
16. For the Exit submenu:

Caption
&Exit

Name
mnuExit
17. Click Next.
18. Click the left arrow () button.
19. Create the Edit menu.

Caption
&Edit

Name
mnuEdit
20. Click Next.
21. Click the right arrow () button to add items for the Edit menu.
22. For the Copy menu item:

Caption
&Copy

Name
mnuCopy

Shortcut
Ctrl+C
23. ClickNext.
24. For the Paste menu item:

Caption
&Paste

Name
mnuPaste

Shortcut
Ctrl+V
25. Click Next.
26. Let's insert a divider.

Caption
- (one dash)

Name
Any name. We won't be referring to this anyway.
27. Click Next.
28. For the Search menu item:

Caption
&Search


Name
mnuSearch

Shortcut
F3
29. Click Next.
30. For the Replace menu item:

Caption
&Replace

Name
mnuReplace

Shortcut
F4
31. Click Next.
32. For the Case Sensitive menu item:

Caption
Case Sensitive

Name mnuCase

Checked
check the checkbox
33. Click Ok.
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.

On your Own
Create a new project with the following menu bar items: Lessons, and Options.
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

The menu

Note: Only one Input Device should be checked at a time. If the user selects Keyboard, then
Mouse should be unchecked.