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.

 


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

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:

 

  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

  1. Click Next.
  2. 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.
  3. For the Open menu item:

§         Caption          &Open

§         Name             mnuOpen

  1. Click Next.
  2. Click the right arrow (è) button to create a submenu.
  3. For the Form submenu:

§         Caption         &Form

§         Name            mnuForm

§         Shortcut        Ctrl+F

  1. Click Next.
  2. For the Project submenu:

§         Caption         &Project

§         Name            mnuProject

§         Shortcut        Ctrl+P

  1. Click Next.
  2. Click the left arrow (ç) button.
  3. For the Close submenu:

§         Caption         &Close

§         Name            mnuClose

§         Enabled         Disable by unchecking the checkbox

  1. Click Next.
  2. For the Exit submenu:

§         Caption         &Exit

§         Name            mnuExit

  1. Click Next.
  2. Click the left arrow (ç) button.
  3. Create the Edit menu.

§         Caption         &Edit

§         Name            mnuEdit

  1. Click Next.
  2. Click the right arrow (è) button to add items for the Edit menu.
  3. For the Copy menu item:

§         Caption         &Copy

§         Name            mnuCopy

§         Shortcut        Ctrl+C

  1. Click Next.

  2. For the Paste menu item:

§         Caption         &Paste

§         Name            mnuPaste

§         Shortcut        Ctrl+V

  1. Click Next.
  2. Let's insert a divider.

§         Caption         - (one dash)

§         Name            Any name.  We won't be referring to this anyway.

  1. Click Next.
  2. For the Search menu item:

§         Caption         &Search

§         Name            mnuSearch

§         Shortcut        F3

  1. Click Next.
  2. For the Replace menu item:

§         Caption         &Replace

§         Name            mnuReplace

§         Shortcut        F4

  1. Click Next.
  2. For the Case Sensitive menu item:

§         Caption       Case Sensitive

§         Name          mnuCase

§         Checked      check the checkbox

  1. 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.


 

 

 


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 ]