Main Files in Applications

The main file in an application can be a program or a form. Though the best choice for a main file is a program (.prg) file, you can combine the functionality of a main program and the user interface that the user initially sees by using a form. When users run your application, Visual FoxPro launches the main file in your application and runs all other components as needed.

If you use a program file as the main file in your application, make sure that it contains commands to handle the major tasks in your application. However, the main file does not need to issue commands directly to accomplish all the tasks. For example, the main file can call procedures or functions to handle tasks such as initializing the environment and cleaning up memory.

The following list contains and describes major tasks and the order to perform them in a main program file:

  1. Initialize the environment for the application by opening databases, declaring variables, and so on. For more information, see How to: Initialize the Environment.

  2. Establish and display the initial user interface by calling a menu or form.

    The initial user interface can be a menu, form, or any other user component. Sometimes, an application displays login box to request user credentials before displaying the opening menu or form.

    You can initiate the user interface in the main program by using the DO command to run a menu or the DO FORM command to run a form. For example, the following line of code runs a form called Startup:

     CopyCode imageCopy Code
    DO FORM BeginApp.scx

    For more information, see DO Command and DO FORM Command.

  3. Detect and respond to user actions by establishing and control the event loop using the READ EVENTS command. For more information, see How to: Control the Event Loop.

  4. End event processing when the user quits the application using a menu command, such as an Exit command, or an Exit button. For more information, see How to: Control the Event Loop.

  5. Restore the environment when the user quits the application.

    Typically, it is recommended that you save default settings of the environment in public variables, a custom class, or as properties of an application object so that you can restore these values when quitting the application.


    If you initialize the environment using a different program than the one you use to restore it, make sure you can access the values you stored. For example, suppose you initialize the environment by calling one procedure but restore the environment by calling another. Make sure to store the values you want to restore in public variables, custom classes, or as properties of an application object.


    Variable names used with macro substitution should not contain the m. prefix because the period assumes variable concatenation and produces a syntax error.

For example, a main program file might contain the following lines of code:

 CopyCode imageCopy Code

These lines of code perform the following tasks:

  • Run a setup program to initialize the environment

  • Run a menu file to display an application menu.

  • Call READ EVENTS to establish the event loop.

  • Run a cleanup program to restore the environment.

See Also

© , 1996-2020 • Updated: 11/10/20
Comment or report problem with topic