Menu System

Using Menus in Your Applications

Users often browse menus before looking elsewhere for information about your application. If your menus are well designed, users can organize their understanding of the application by developing a mental model based on the menu organization and content alone. With the Visual FoxPro Menu Designer, you can create menus that enhance the quality of your applications.
Each part of a Visual FoxPro application can have its own menu system, or set of menus. The following sections describe how to create a menu system, but don't explain how to incorporate the menu system into your application. For details about adding menus to an application, see Chapter 13, Compiling an Application.
The following sections describe:
  • Creating a Menu System
  • Planning a Menu System
  • Creating Menus, Shortcut Menus, Menu Items, and Submenus
  • Assigning Tasks to a Menu System

Creating a Menu System

Much of the work involved in creating a menu system is done in the Menu Designer, where you create the actual menus, submenus, and menu options.
Menu Designer
Creating a menu system involves several steps. Regardless of the size of your application and the complexities of the menus you plan to use, you should:
  • Plan and design the system.
    Decide what menus you need, where they appear in the interface, which ones need submenus, and so on. For more information about planning menu systems, see Planning a Menu System later in this chapter.
  • Create the menus and submenus.
    Define the menu titles, menu items, and submenus using the Menu Designer.
  • Assign tasks to the system so that it does what you want.
    Specify tasks for the menus to perform, such as displaying forms and dialog boxes. Additionally, include setup code and cleanup code if appropriate. Setup code executes before the menu system is defined and can include code for opening files, declaring variables, or placing the menu system on a stack so that it can be retrieved later. Cleanup code contains code to be executed after the menu definition code, and makes menus and menu items available or unavailable for selection.
  • Generate the menu program.
  • Run the program to test the system.

Planning a Menu System

The usefulness of an application can depend on the quality of its menu systems. If you invest planning time in your menus, users will accept them readily and learn them quickly.
While designing your menu system, consider the following guidelines:
  • Organize the system according to the tasks users will perform, not according to the hierarchy of programs in the application.
    Users can form a mental model of how the application is organized by looking at the menus and menu items. To design these menus and menu items effectively, you should know how your users will think about and accomplish their work.
  • Give each menu a meaningful title.
  • Organize menu items according to their expected frequency of use, their logical sequence, or their alphabetical order.
    If you can�t predict the frequency and can�t determine a logical order, then organize menu items alphabetically. Alphabetical ordering is particularly effective when a menu contains more than eight items. With so many items, the user spends time scanning them; alphabetical ordering facilitates scanning.
  • Put separator lines between logical groups of menu items.
  • Limit the number of items on a menu to one screen.
  • If the number exceeds the length of a screen, create submenus for appropriate menu items.
  • Choose access keys and keyboard shortcuts for menus and menu items.
    For example, ALT+F could be an access key for a File menu.
  • Use words that clearly describe the menu items.
    Use common words rather than computer jargon, and use simple, active verbs to indicate what actions will result from choosing each menu item. Don�t use nouns as verbs. Additionally, describe the menu items using parallel construction. For example, if you use single words for all the items, then use the same part of speech for all of them.
  • Use mixed upper and lowercase letters in menu items.
    Tip   To see an example of a menu system, run the Tasmanian Traders application, Tastrade.app, located in the Visual Studio �\Samples\Vfp98\Tastrade directory.

Creating Menus, Shortcut Menus, Menu Items, and Submenus

After planning your menu system, you can create it with the Menu Designer. You can create menus, shortcut menus, menu items, submenus of menu items, lines that separate groups of related menu items, and so on. The following sections provide the details.

Creating Menus

You can create menus by customizing the existing Visual FoxPro menu system or by developing your own menu system. To start with the existing Visual FoxPro menu system, use the Quick Menu feature.
To create a menu system with Quick Menu
  1. From the Project Manager, select the Other tab, select Menus, and then select New.
  2. Select Menu.
    The Menu Designer appears.
  3. From the Menu menu, choose Quick Menu.
    The Menu Designer now contains information about the main Visual FoxPro menus.
    A menu system created with the Quick Menu feature
  4. Customize the menu system by adding or changing menu items.
    For example, insert a Customer menu before the Help menu by choosing the mover button associated with the Help menu, choosing the Insert button, and then typing Customer in the Prompt column. The result looks like this:
    A customized menu system
Tip   Drag mover buttons to change the location of menus on the menu bar.
If you need a Help menu, make it the last menu on the menu bar so that users can find it quickly.
Before you can use your menu in an application, you must generate it.
To generate a menu
  • From the Menu menu, choose Generate.
Visual FoxPro prompts you to save the menu system in a file with an .mnx extension. This file is a table that will store all the information about the menu system. After you save the menu system, Visual FoxPro prompts for an output file with an .mpr extension. This file will contain the generated menu program.

Creating Shortcut Menus

Shortcut menus appear when you click the right mouse button on a control or object, and provide a quick way to expose all of the functions that apply to just that object. You can use Visual FoxPro to create shortcut menus, then attach these menus to controls. For example, you can create a shortcut menu containing the commands Cut, Copy, and Paste that will appear when a user right-clicks on data contained in a Grid control.
To create a shortcut menu
  1. From the Project Manager, select the Other tab, select Menus, and then choose New.
  2. Choose Shortcut.
    The Shortcut Designer appears.
Once you are in the Shortcut Menu designer, the process for adding menu items is the same as it is for creating menus.
For an example of shortcut menus, run Solution.app in the Visual Studio �\Samples\Solution\Vfp98\Solution directory.

Creating SDI Menus

SDI menus are menus that appear in single-document interface (SDI) windows. To create an SDI menu you must indicate that the menu will be used on an SDI form while you're designing the menu. Other than that, the process of creating an SDI menu is the same as that for creating a normal menu.
To create an SDI menu
  • While the Menu Designer is open, choose General Options from the View menu, and select Top-Level Form.

Creating Menu Items

After creating menus, you can place menu items on the menus. Menu items can represent Visual FoxPro commands or procedures that you want the user to execute, or menu items can contain submenus that offer additional menu items.
To add menu items to a menu
  1. In the Prompt column, select the menu title to which you want to add menu items.
  2. In the Result box, select Submenu.
    Create button appears to the right of the list.
  3. Choose the Create button.
    An empty design window appears. In this window, you enter the menu items.
  4. In the Prompt column, type the names of the new menu items.

Creating Submenus

For each menu item, you can create a submenu containing additional menu items.
To create a submenu
  1. In the Prompt column, select the menu item to which you want to add a submenu.
  2. In the Result box, select Submenu.
    Create button appears to the right of the list. If a submenu already exists, an Edit button appears instead.
  3. Select Create or Edit.
  4. In the Prompt column, type the names of the new menu items.

Adding Menus Programmatically

Though you typically create menus and menu items using the Menu Designer, you can also create them using Visual FoxPro commands. For example, you can create a menu using DEFINE PAD, a submenu using DEFINE POPUP, and items on the submenu using a series of DEFINE BAR commands.

Grouping Menu Items

For readability, separate groups of similar menu items with dividing lines. For example, in Visual FoxPro, the Edit menu has a line separating the Undo and Redo commands from the Cut, Copy, Paste, Paste Special, and Clear commands.
Grouped menu items
To group menu items
  1. In the Prompt column, type \-. This creates a dividing line.
  2. Drag the button to the left of the \- prompt to move the dividing line until it appears where you want it.

Saving a Menu as HTML

You can use the Save As HTML option on the File menu when you're creating a menu to save the contents of a menu as an HTML (Hypertext Markup Language) file.
To save a form as HTML
  1. Open the menu.
  2. Choose Save As HTML on the File menu. (You will be asked to save the menu if it has been modified.)
  3. Enter the name of the HTML file to create and choose Save.

Including Menus in an Application

Once you create a menu system, you can include it in your application.
To include a menu system in your application
  • Add the .mnx file to your project, then build the application from the project. For more information about building your application, see Chapter 13, Compiling an Application.

Attaching Shortcut Menus to Controls

Once you create and generate a shortcut menu, you can attach it to a control. Shortcut menus typically appear when a user right-clicks on a control. You can attach a shortcut menu to a particular control by entering a small amount of code in the right-click event of the control.
  1. Select the control to which you want to attach the shortcut menu.
  2. In the Properties window, choose the Methods tab and select Right Click Event.
  3. In the code window, type DO menu.MPR, where menu is the name of the shortcut menu.
    Note   Be sure to use the .mpr extension when referencing shortcut menus.

Attaching SDI Menus to Forms

Once you create an SDI menu, you can attach it to an SDI form. In addition, you must:
  • Set the form�s ShowWindow property.
  • Add a DO statement to the form�s Init event.
To attach an SDI menu to a form
  1. In the Form Designer, set the form�s ShowWindow property to 2 � As Top Level Form.
  2. In the Init event of the form, call the menu.
    For example, if your menu is called SDIMENU.MPR, add this code:
    DO SDIMENU.MPR WITH THIS,.T. 

Assigning Tasks to a Menu System

As you create a menu system, you should consider ease of access to the system, and you must assign tasks to the system. You must give menus and menu items tasks to perform, such as displaying forms, toolbars, and other menu systems. You should define access keys to permit entry to the menu system. You can also add keyboard shortcuts and enable or disable menu items for more control.

Assigning Access Keys

Well-designed menus have access keys for quick keyboard access to the menu functionality. The access key is represented by the underlined letter in the menu title or menu item. For instance, the Visual FoxPro File menu uses �F� as its access key.
If you don�t assign an access key to a menu title or menu item, Visual FoxPro automatically assigns the first letter as the access key. For example, the Customer menu created previously didn�t have a defined access key. Accordingly, Visual FoxPro assigned the first letter (C) as the access key.
Menus with access keys
To specify the access key for a menu or menu item
  • Type \< to the left of the letter you want as the access key.
    For example, to set the access key to �u� in the Customer menu title, replace Customer with C\<ustomer in the Prompt column.
Troubleshooting   If an access key for your menu system doesn�t work, look for duplicate access keys.

Assigning Keyboard Shortcuts

In addition to assigning access keys, you can specify keyboard shortcuts for menus or menu items. As with access keys, keyboard shortcuts let you choose a menu or menu item by holding down one key while pressing another. The difference between access keys and keyboard shortcuts is that you can use a keyboard shortcut to choose a menu item without first displaying its menu.
Keyboard shortcuts for Visual FoxPro menu items are combinations of the CTRL or ALT key and another key. For instance, you can create a new file in Visual FoxPro by pressing CTRL+N.
To specify a keyboard shortcut for a menu or menu item
  1. In the Prompt column, select the appropriate menu title or menu item.
  2. Choose the button in the Options column to display the Prompt Options dialog box.
  3. In the Key Label box, press a key combination to create a keyboard shortcut.
    If a menu item doesn�t have a keyboard shortcut, Visual FoxPro displays �(press the key)� in theKey Label box.
  4. In the Key Text box, add the text you want to appear beside the menu item.
    By default, Visual FoxPro repeats the keyboard shortcut from the Key Label box in the Key Text box. However, you can change the text in the Key Text box if you want your application to display different text. For example, if both the Key Label and Key Text were CTRL+R, you could change the Key Textvalue to ^R.
    Note   CTRL+J is an invalid keyboard shortcut because it is used to close certain dialog boxes in Visual FoxPro.

Enabling and Disabling Menu Items

You can enable or disable a menu or menu item based on a logical condition.
To enable or disable a menu or menu item
  1. In the Prompt column, select the appropriate menu title or menu item.
  2. Choose the button in the Options column to display the Prompt Options dialog box.
  3. Select Skip For.
    The Expression Builder appears.
    Expression Builder Dialog Box
  4. In the Skip For box, type the expression that determines whether the menu or menu item is enabled or disabled.
    If the expression evaluates to false (.F.), the menu or menu item is enabled. If the expression evaluates to true (.T.), the menu or menu item is disabled and cannot be selected or chosen. For details, see DEFINE BAR and DEFINE PAD.
    Note   After the menu system has been displayed, you can enable and disable menus and menu items by using the SET SKIP OF command..

Marking the State of a Menu Item

On a menu, a check mark next to a menu item indicates that it is in effect. For example, if you put a check mark next to the Credit item on the Customer menu created earlier, Credit is in effect.
At run time, you can place a check mark next to a menu item by using the SET MARK OF command.
For an example of disabling and marking the state of menu items, run Solution.app in the Visual Studio �\Samples\Vfp98\Solution directory.

Assigning Tasks to Menus or Menu Items

When a menu or menu item is selected, it performs a task, such as displaying a form, a toolbar, or another menu system. To perform a task, a menu or menu item must execute a Visual FoxPro command. The command can be contained in one line, or it can be a procedure call.
Tip   If you expect to use a set of commands in several places, write a procedure. The procedure should be explicitly named and written in the menu cleanup code, or somewhere where it can be referenced by any menu or object.

Performing Tasks with Commands

To perform a task, you can assign a command to a menu or menu item. A command can be any valid Visual FoxPro command, including a call to a program that exists on your path or a procedure defined in the Cleanup option of the General Options dialog box. For more information, see Creating a Default Procedure for a Menu System later in this chapter.
To assign a command to a menu or menu item
  1. In the Prompt column, select the appropriate menu title or menu item.
  2. In the Result box, select Command.
  3. In the box to the right of the Result box, type the appropriate command:
    Assigning a command to a menu
If the command calls a procedure in the menu�s cleanup code, use the DO command with the following syntax:
DO procname IN menuname
In this syntax, menuname specifies the location of the procedure. This is the name of the menu file and must have the .mpr extension. If you don�t specify the location in menuname, you must specify it with SET PROCEDURE TO menuname.mpr, if the procedure is in the menu cleanup code.

Displaying Forms and Dialog Boxes

From a menu or menu item, you can display a compiled form or dialog box by calling it with a command or procedure. For example, to display a form named �Orders,� use the following command:
DO FORM Orders 
Tip   When you create a menu or menu item that displays a form or dialog box, put three dots at the end of the prompt to indicate that more user input is required.
Dots after a menu item show that user input is required.