Adding a Context Menu

   Tasks

Finding the right menu command to use can sometimes be difficult when an application has a complex menu system. It may also be important to provide commands that apply to specific controls on your form and thus are not appropriate for displaying on main menus. Use context menus to display when the user clicks the right mouse button over a form or control.

Typically context menus are combinations of menu commands found throughout a form's main menu. They can also be custom menus that do not have a corresponding menu item on a main menu. For this reason, Visual J++ provides the ContextMenu control. The same menu designer that is used for main menus is used to create context menus.

To add a context 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 ContextMenu control to add it to your form.

Once you have added the ContextMenu control to your form, you can then double-click it to display the menu designer for the context menu. The menu designer that is displayed for context menus is identical to the one used to create main menus. The only difference is that you do not create a top-level menu item for a context menu because context menus are displayed as a consolidated list of menu items. For information on how to create menu items for a context menu, see Adding and Modifying a Menu.

Copying Menu Items to a Context Menu

Because most context menus are a collection of menu items that are found on a main menu, you can copy menu items from your main menu and paste them onto your context menu. Although copying the menu items from the main menu creates additional MenuItem objects on your context menu, copying the menu items makes creating your context menu easier.

To copy menu items from a main menu to a context menu

  1. Click the MainMenu control that contains the menu item that you want to copy.

  2. In the menu designer, click the menu item to be copied.

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

  4. Double-click the ContextMenu control that you want to paste the menu item into.

    The context menu is displayed in the menu designer.

  5. Click the menu item that is located below the location where you want to paste the menu item.

    The menu designer creates a new MenuItem object with the same properties as the main menu item that you copied and associates it with your context menu.

Assigning a Context Menu to a Control

After you have designed your context menu, you need to assign it to a form or control so that it can be displayed.

To assign a context menu to a control

  1. Click the form or control that you want to assign a context menu to.

  2. In the Properties window, select the contextMenu property of the form or control.

  3. From the list, select the context menu that you want to assign to the control.