How to Attach a Menu to a Form

Summary

This article shows by example how to add a menu to a Visual FoxPro form so that the menu appears on the form rather than in the default position across the top of the screen.

More Information

In Visual FoxPro, menus are not objects that can be instantiated (created) on or in a form. Nevertheless, you can add a menu directly to the form so that it appears on the form underneath the title bar rather than in the default system menu position across the top of the screen. To do this, use the DEFINE MENU command as shown in the following example.

Step-by-Step Example

  1. Create a form named Form1.
  2. In the Activate event procedure for the form, include the following code to define the menu bar, and menu lists. Include the ACTIVATE MENU command.
       CLEAR
    SET SYSMENU SAVE
    SET SYSMENU TO
    ON KEY LABEL ESC KEYBOARD CHR(13)
    DEFINE MENU example BAR in window FORM1
    DEFINE PAD convpad OF example PROMPT '\<Conversions' COLOR SCHEME 3;
    KEY ALT+C, ''
    ON PAD convpad OF example ACTIVATE POPUP conversion
    DEFINE POPUP conversion MARGIN RELATIVE SHADOW COLOR SCHEME 4
    DEFINE BAR 1 OF conversion PROMPT 'Ar\<ea' KEY CTRL+E, '^E'
    DEFINE BAR 2 OF conversion PROMPT '\<Length' KEY CTRL+L, '^L'
    DEFINE BAR 3 OF conversion PROMPT 'Ma\<ss' KEY CTRL+S, '^S'
    DEFINE BAR 4 OF conversion PROMPT 'Spee\<d'KEY CTRL+D, '^D'
    DEFINE BAR 5 OF conversion PROMPT '\<Temperature' KEY CTRL+T, '^T'
    DEFINE BAR 6 OF conversion PROMPT 'T\<ime' KEY CTRL+I, '^I'
    DEFINE BAR 7 OF conversion PROMPT 'Volu\<me' KEY CTRL+M, '^M'
    ON SELECTION POPUP conversion DO choice IN defimenu;
    WITH PROMPT(), POPUP()
    ACTIVATE MENU example nowait
  3. In the Unload event procedure of the form, include the following code to deactivate the menu and restore the default system menu.
       DEACTIVATE MENU example
    RELEASE MENU example EXTENDED
    SET SYSMENU TO DEFAULT
    ON KEY LABEL ESC
  4. Save and run the form. A menu bar with a single option (Conversion) will appear just below the title of the form. The default system menu bar will disappear. When the form is released, the defined menu will also be released and the default system menu bar restored.
NOTE: It is possible to use code generated by the Menu Designer, but you would need to edit the generated code before using it. The generated menu is not a new menu; it is still the system menu. You must alter the code to specifically name and define the menu, to activate the menu, and to save and clear the system menu. It is easier to generate the menu, copy and paste the generated menu code into the Activate event procedure code, and then modify the code there.


Any procedures executed by individual menu items are best included in the existing procedure files for the project or database.
Lastnosti

ID članka: 135517 – Zadnji pregled: 20. jun. 2008 – Revizija: 1

Povratne informacije