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
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.
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
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.
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
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.
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
-or-
In the Properties window, click the shortcut property and select a key combination.
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
-or-
In the Properties window, set the checked property to true.
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
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.
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
The menu item moves and snaps to place. Other menu items that are affected by this change are also moved.
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
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.
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.
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
Instead of displaying a caption for each menu item, the menu designer now displays the instance name for each menu item.
When you have completed setting all the menu items, you can follow the first two steps to switch back to editing menu item captions.