Freigeben über


Walkthrough: Creating Applications with Visual FoxPro

You can use Visual FoxPro to create simple programs or more complex enterprise level applications, servers, and XML Web services. You can use tools to create programs or manually write them. When you compile Visual FoxPro programs, you can compile them as application (.app) files for running in a Visual FoxPro environment or as executable (.exe) or dynamic-link library (.dll) files that you can call from anywhere in a Microsoft Windows environment.

The following sections in this walkthrough explain how to create a simple program and use it as the basis for an application:

  • Using Data

  • Creating a Program for a Form

  • Creating an Application

Using Data

Visual FoxPro programs and applications are based on data in tables or cursors. Data can exist already on your hard disk, a remote server, or a Web site, or you can create it in Visual FoxPro. Data can be in any of the formats that Visual FoxPro recognizes, including OLE objects.

For this sample application, the data you use will be copied from the Orders table, which is included with Visual FoxPro in the \Program Files\Microsoft Visual FoxPro 9\Samples\Data folder.

To move data from one table to another

  1. On the File menu, click Export.

  2. In the Type drop-down list in the Export dialog box, select Visual FoxPro 3.0 (DBF).

  3. In the To text box, type myApp as the name of the file you want to create.

  4. Click the ellipsis (...) button and save the file in the Visual FoxPro Projects folder (..\My Documents\Visual FoxPro Projects).

  5. In the From text box in the Export dialog box, click the ellipsis (...) button, and locate the Orders.dbf table in \Program Files\Microsoft Visual FoxPro 9\Samples\Data folder.

  6. In the Export dialog box, click Options .

  7. In the Export Options dialog box, click Fields, move cust_id, to_city, to_country, and order_amt from the All fields list to the Selected fields list, and then click OK. Click OK twice more to close the Export Options dialog box and the Export dialog box.

  8. On the File menu, click Open.

  9. In the Files of type drop-down list, choose Table, select the open exclusive check box, and browse to myApp.dbf.

  10. On the View menu, click Table Designer.

  11. On the Fields tab, select the to_city row, click the Index drop-down list, and choose Ascending.

  12. Click OK, and click Yes to make the structure change permanent.

  13. On the Window menu, click Data Session to order the table according to your new index.

    Note

    You have created a table that, because it is indexed, supports group calculations in the Report Designer.

  14. In the Data Session window, click Properties.

  15. In the Index Order drop-down list, select myApp:to_city, and then click OK.

    You have now exported selected fields from one table to another and then set an index on the new table. Leave the Data Session window open, and use this table immediately for the rest of this sample.

Creating a Program for a Form

You can create simple Visual FoxPro programs from the menus and the IDE. These programs either run in Visual FoxPro or access the Visual FoxPro Runtime.

To create the form

  1. On the File menu, click New.

  2. Click Form, and then click New file.

    This opens the Form Designer, adds the Form menu and the Form Controls toolbar, and displays the Properties window for the form. If the toolbar is not visible, you can open it from the View menu.

  3. In the Properties window for the form, select Caption, and type myForm.

To add a label

  1. On the Form Controls toolbar, click Label, and drag a rectangle on the form.

  2. In the Properties window for the label control, select Caption, and change it to My Data Form. Then, select FontSize, and type 20.

  3. If necessary, resize the label so the text fits.

To add a grid

  1. On the Form Controls toolbar, click Grid, and then draw a rectangle on the form.

  2. Right-click the Grid control, and select Builder from the shortcut menu.

  3. In the Grid Builder, select the table, myApp.

  4. On the Grid Items tab, move all the items in the Available fields list to the Selected fields list, and then close the builder.

To add a button

  1. On the Form Controls toolbar, click the Command Button icon, and then draw a rectangle on the form below the grid.

  2. Double-click the newly created button, and type the following code in the code editor. Close the code editing window when you are finished.

    THISFORM.RELEASE
    

    This code will close the form when you do not need it any more.

  3. In the Properties window for the button control, select Caption, and type Exit.

  4. On the File menu, click Save As, and save the form as myForm.

  5. Click Run (!) on the toolbar to preview your form.

To complete the program

  1. On the File menu, click New.

  2. Click Program, and then click New file. This opens the Visual FoxPro editor.

  3. In the editor, type the following:

    DO FORM myForm.scx
    
  4. Save this program as myProgram. This creates a Visual FoxPro program (.prg) file that runs your form from the following command in the Command window:

    DO myProgram
    

Now you have placed a table (as a grid), a label, and a button on a form. You created a program to run from the Command window or Program menu. In the next section, the application exchanges a data review form for the grid and adds it to the program before compiling it to a file.

Creating an Application

Running the program you created earlier requires Visual FoxPro to be installed on the computer. However, you can create an application with Visual FoxPro that will run on a computer that does not have Visual FoxPro installed on it.

The earlier program has only one form and does not have much functionality. This section walks you through adding a data review form with some additional controls and then compiling the two forms — first into a Visual FoxPro application and then into a Windows application. A Visual FoxPro application (.app) file or a Visual FoxPro executable (.exe) file can each run on Windows platforms if Visual FoxPro is installed. A Visual FoxPro application compiled as an executable (.exe) file also can run on Windows platforms with only the Visual FoxPro Runtime installed.

In this section, you will create an application using the Application wizard, the Application builder, and the Project manager.

  • The Application wizard creates a project and provides the advanced application framework so you can add components you might have created already or create an application from start to finish, including splash screens and other enhancements.

  • The Application builder makes it possible for you to add tables, forms, and reports to an existing project.

  • The Project manager, the most fundamental application-building tool in Visual FoxPro, provides access to all the components you might want to include in an application and contains them for compilation.

For this walkthrough, you use the Application wizard first.

To create an application

  1. On the Tools menu, point to Wizards, and click Application.

  2. In the Project name box, type MyApplication, save the project in your Visual FoxPro Projects folder (..\My Documents\Visual FoxPro Projects), and then click OK.

Visual FoxPro prepares to create a project file with the name you specified. When it has finished creating the project, two windows appear: the Project Manager and the Application Builder. In the following procedures, you use the Application Builder to add components and functionality to your application.

To specify splash screen and About dialog box information

  1. On the General tab of the Application Builder, click the ellipsis (...) button next to the Image box.

  2. Find the Visual FoxPro logo, fox.bmp, in your Visual FoxPro folder (..\Program Files\Microsoft Visual FoxPro VersionNumber), and click OK. This image will appear on your application splash screen.

    Note

    If you close the Application builder, you can reopen it by clicking the Tools menu, pointing toWizards, and clicking All Wizards. Click Application Builder in the Wizard Selection dialog box, and then click OK.

  3. On the Credits tab of the Application Builder, add the appropriate authoring and version information.

To specify the data source

  • On the Data tab, click Select, and find and select myAPP (the table you used earlier in the simple program).

    Note

    You can choose any available table. This tab also provides buttons, so you can create tables or a database easily.

To create a form and a report

  • On the Data tab, you can select from lists of form and report templates that are part of the Advanced Application Framework (included in the Application Builder). Accept the default values in the drop-down lists, and then click Generate.

    Visual FoxPro generates a form and a report from the table you specify and gives you access to them from the Forms and Reports tabs of the Application builder. These components become part of the project you are building, and the files are saved in the Forms or Reports folder in your Visual FoxPro Projects folder (..\My Documents\Visual FoxPro Projects).

To add controls to the form

  1. On the Forms tab of the Application Builder, click Edit to open the Form Designer, where you can make any additions or changes.

    As is, the form will display only the first record, so you must add at least one more control; in this procedure, you add two.

  2. On the Form Designer toolbox, click the Button icon, and drag a rectangle in the form. In the Caption property, type Next.

  3. Double click the Next button on the form, and type the following code in the code editor.Close the code-editing window when you are finished.

    IF NOT EOF()
       SKIP
    ELSE
       GO TOP
    ENDIF
    THISFORM.REFRESH
    

    This code steps through the table records and displays each record. You also could add another button, with appropriate code, to move back through the table.

  4. Copy the Next button, and place the copy at the far right of the form. In the Caption property, type Exit.

  5. Double click the Exit button, and, in the Click event edit window, replace the code in the window with the following code, and then close the window:

    THISFORM.RELEASE
    

    This code closes your form.

To add calculations to the report

  1. On the Reports tab of the Application Builder, click Edit for MyApp to open the Report Designer. Expand all the bands of the report to make it easier to perform the following operations.

    Note

    This is where you can make any additions or changes, such as rearranging fields or adding controls.

  2. On the View menu, click Data Environment.

  3. Right click the Data Environment window, and select Properties from the shortcut menu.

    This will set the Properties window to the Dataenvironment.

  4. In the Properties window, choose the cursor from the Properties Description drop-down list, and change the Order property to to_city to make grouping possible.

  5. On the Report menu, click Data Grouping.

  6. In the Data Grouping dialog box, click the Add button and then double-click to_city in the Fields list in the Expression Builder. Click OK in both dialog boxes.

  7. In the Report Designer, copy and paste the to_city element, and then drag the pasted element from the Detail band to the Group Footer band for group totals. Repeat this procedure to place another copy of the to_city element in the Page Footer for the entire table.

  8. For each copy of the to_city element, double-click the element to open the Report Expression dialog box.

  9. Click Calculations.

  10. In the Calculation Field dialog box, click Count.

    The Reset drop-down list should display the default selection, which is the to_city field. This means the Count function will reset to zero each time the value of the to_city element changes in the pass through the table. This is correct for the Group Footer band, but you must change this selection for the to_city element in the Page Footer band.

  11. After you select Count in the Calculate Field dialog box of the to_city element you placed in the Page Footer band, change the Reset value to End of Report.

  12. After setting the Calculation field of each to_city element, click OK to close the dialog box.

  13. Click OK to close the Application Builder.

Note

In addition to the features you specified in the Application wizard, Visual FoxPro created other components for your application. When you run your application, you will discover a customized File menu and a dialog box from which you can run your form and your report.

Now you can use the Project manager to review the components you have created. You will find that the Application wizard created many components, which you modified in the Application builder and the Form and Report designers. You can examine and modify these components in various Visual FoxPro tools, such as the designers or the Class Browser and Object Browser.

At this point, the most important use of the Project manager is to complete the compilation of your files into an application.

To complete the application

  1. In the Project Manager, click Build.

  2. Click Application (app) to create an application to be run only in Visual FoxPro.

    Note

    If you select Win 32 executable / COM server (exe), you create an application that can be run on a computer with only the Visual FoxPro Runtime installed.

  3. Click OK.

When the build process is complete, you have an application you can run in Visual FoxPro by double-clicking the .app file. When the program opens, it displays the splash screen and then the Quick Start dialog box from which you can view your form and your report. The application also provides a Quick Start menu item.

For more information on creating programs and applications, see Application Wizard and Application Builder.

For more information on the Project Manager and for details on application build options, see Project Manager Window, and Build Options Dialog Box.

See Also

Reference

Application Wizard
Application Builder

Other Resources

Walkthroughs