Adding and Modifying a Menu

   Tasks

Menus are a convenient and consistent way for your application to provide a set of commands to users. In most applications, a menu is the gateway to the many available commands and features. Menus also provide a great mechanism for displaying the state of your application. This is performed by disabling and enabling menu item commands that are applicable to your application's current state. There are two types of menus that can be created in Visual J++: main menus and context menus.

Main menus are displayed on a menu bar that is located immediately below the title bar on a form. The menu bar contains top-level menu items that are used to group related menu item commands. For example, clicking a File top-level menu item displays menu items that are related to file operations. Menu items are typically used to display commands for your application (such as New and Exit), but they can also be used to display separator bars and submenu items. You can also use check marks next to menu items to display the state of a command or a feature in your application. To create menus for your forms easily, Visual J++ provides the MainMenu control and the menu designer.

Context menus can be displayed for a specific control or area of your form. They are typically accessed using the right mouse button. For more information on using context menus, see Adding a Context Menu.

To add a main menu to a form

  1. In the Forms Designer, open the form that you want add a menu to.

  2. Under the WFC Controls tab in the Toolbox, double-click the MainMenu control.

    The MainMenu control is inserted onto your form, a menu bar is added to the top of your form, and the menu designer is started. The menu designer displays a menu item box with the words "Type Here" on the menu bar in the location of the first top-level menu item. When you add the MainMenu control to the form, the menu property for the form is set to the name of your MainMenu control. If you add other MainMenu controls, you must change the MainMenu property of the form to the MainMenu control that you want displayed.

Adding a Menu Item

After you have added the MainMenu control to your form, you define top-level menu items and their related menu items.

To add a menu item to a menu

  1. If the menu designer is not currently displayed, click the MainMenu control that you added to display it.

  2. Click the box on the menu bar that contains the words "Type Here."

  3. Type the caption for the menu item, and press ENTER.

    After you have completed the caption, the menu designer adds the necessary code to create a menu item and associate it with your menu. The menu item box moves below your new top-level menu item so that you can add additional items. Use the same process to add additional menu items. In addition to the menu item box for adding submenu items, the menu designer adds a box to the right of your menu item so that you can add additional top-level menu items.

When you create a menu item, an instance of a MenuItem object is created. Each menu item has properties that you can view and set and events that you can handle. In fact, you can consider menu items as small controls that are contained within a MainMenu control. Menu items can be enabled or disabled, checked or unchecked, made visible or hidden using properties for each menu item. Because most menu items are commands that begin a process or change the state of your application, you must create event handlers for your menu items. Setting the properties of a menu item and creating event handlers is the same process as performing these operations on a form or control.

Adding Access Keys

To make your application accessible to people who do not have or who cannot use a mouse, you can provide access keys for your menu items. Access keys provide a way for the user to access the menu using only the keyboard. An access key corresponds to an underlined letter on the menu.

To add access keys to a menu

  1. Click the menu item to which you want to add an access key.

  2. Place the cursor immediately before the letter in the caption that you want to be your access key letter and type an ampersand (&).

    For example, on a File menu, click the File menu item to edit the caption, place the cursor before the "F" in File, and then type an ampersand. You can also edit the caption title by changing the text property for the menu item in the Properties window.

Adding Shortcut Keys

In addition to providing access key for your menu items you can also add shortcut keys. A shortcut key is a keyboard combination, such as CTRL+C, that invokes a particular command.

To add a shortcut key to a menu item

  1. In the menu designer, click to the immediate right of the menu item caption to which you want to add a shortcut key.

  2. In the drop-down list, select a key combination.

    -or-

    In the Properties window, click the shortcut property and select a key combination.

Displaying a Check Mark

At times, you may want to give users a choice of commands that they can apply to the application. For example, there may be several available toolbars. The user can select which toolbar will be displayed by clicking the its name on the menu. A check mark appears next to the toolbar name.

To display a check mark on a menu

  1. Select a menu item.

  2. Click to the immediate left of the menu item name. A check box appears.

    -or-

    In the Properties window, set the checked property to true.

Grouping Menu Items

Although the top-level menu items allow you to group related menu item commands, you may find it necessary to group menu items on a menu. For example, on most File menus, file operations such as Open and Save are separated from print-related operations. You can separate groups of menu items by using a separator bar, which is a three-dimensional line between two menu items.

To create a menu separator bar

  1. Click the menu item that is below the location where you want to add a separator bar.

  2. Click the right mouse button, and then click Insert Separator on the context menu.

    A separator bar appears directly above the currently selected menu item. Separator bars can be moved and deleted in the same way as other menu items.

Moving a Menu Item

After you have created a menu item, you can move it to another location in the list or to another section of the menu.

Note   Moving top-level menu items moves all submenus. However, submenus are not displayed during the move.

To move a menu

  1. Select the menu item that you want to move.

  2. Drag the menu item to the new position.

    The menu item moves and snaps to place. Other menu items that are affected by this change are also moved.

Copying Menu Items

In addition to moving menu items, you can also copy and paste them onto other parts of your menu structure.

To copy and paste a menu item

  1. Select the menu item that you want to copy.

  2. Click the right mouse button and then click Copy.

  3. Select the menu item that is below where you want the menu item to be pasted.

  4. Click the right mouse button and then click Paste.

    A copy of the menu item is made at the specified location. This includes all properties that were specified for the original menu item, except the name property. When you copy a menu item, a new instance of the MenuItem class is created.

Deleting a Menu Item

After creating a menu system for your form, you may find that certain menu items are no longer needed. If the menu item that you are deleting contains submenu items, a message box is displayed to confirm that you want to delete the menu item and its submenus. If you delete a menu item, any event handlers that are associated with that menu item are not removed from your form's class definition. You must remove them manually.

To delete a menu item from your form

Tip   To delete the entire menu, select each menu caption and press DELETE, or click the top-level menu and press DELETE.

Editing a Menu Item's Name

Because each menu item is also a MenuItem object, a name is associated with the menu item in addition to its caption. By default, the name is menuItem[number], where number indicates the number of the menu item. Although you can change each menu item's name property using the Properties window, it is easier to change the name using the Edit Name mode in the menu designer.

To edit the name of a menu item using the menu designer

  1. Click the right mouse button over a menu item.

  2. On the context menu, click Edit Names to toggle the Edit Names mode for the menu.

    Instead of displaying a caption for each menu item, the menu designer now displays the instance name for each menu item.

  3. Click the name associated with the menu item you want to modify (for example, menuItem1), and type a new name.

    When you have completed setting all the menu items, you can follow the first two steps to switch back to editing menu item captions.