Special Edition Using Visual FoxPro 6


Chapter 18

The Visual FoxPro Foundation Classes


Familiarizing Yourself with the Visual FoxPro Foundation Classes

This chapter introduces you to a new feature of Visual FoxPro 6, the Visual FoxPro Foundation Classes. One major advantage of adopting object-oriented programming (OOP) technology is that OOP promotes object reusability to facilitate rapid application development (RAD). The only problem is determining where to get the class objects to reuse. In the past, you could reuse class objects that you developed for previous tasks, borrow them from your friends, or buy them from a third-party source. The new Visual FoxPro Foundation Classes that ship with Visual FoxPro contain over 100 reusable classes that you can add to your application to speed up and simplify your development effort while reducing the amount of code you have to write.

The Visual FoxPro Foundation Classes consist of a group of classes that provide a wide range of operations that most typical applications need. The classes that are provided support the following functionality:

The Visual FoxPro Foundation Classes are stored as .VCX class libraries. You can add foundation classes to your applications to provide powerful functionality with little or no programming. Furthermore, you are free to distribute the foundation classes with your applications.

You can even load them in the Class Browser or Class Designer and examine all the structures and code. Not only do you find out how the classes operate, but you also can learn some advanced Visual FoxPro programming techniques.

Table 18.1 describes what each of the foundation classes does and identifies the associated visual class library file (.VCX). You will learn how to use these foundation classes in your applications later in this chapter.

Table 18.1  Visual FoxPro Foundation Classes
Class Description Class Library File
About Dialog Adds an About dialog box to your project. _dialogs.vcx
ActiveX Calendar Adds a monthly calendar to your form to which you can bind date data fields. _datetime.vcx
Application Registry Provides a set of Registry functions that return application-related information. registry.vcx
Array Handler Provides methods that you can use to do certain array operations that are not performed by Visual FoxPro array functions, such as insertion and deletion of array elements and column scanning. _utility.vcx
Cancel Button Provides a button control that releases a form and discards any associated buffered data changes. _miscbtns.vcx
Clock Provides a control that displays the current time on a form. _datetime.vcx
Conflict Catcher Displays a dialog box that shows rows with conflicting values during an edit session using optimistic row or table buffering. The dialog box displays the original, current, and new values. You can choose the value to use. _dataquery.vcx
Cookies Class Used with MS Information server such as FoxISAPI to manipulate Internet cookies. _internet.vcx
Cross Tab Control generates cross-tab report. _utility.vcx
Data Edit Buttons Navigation bar component that includes Top, Previous, Next, Bottom, Find, Print, Add, Delete, Edit, and Save buttons. Wizbtns.vcx
Data Navigation Buttons A group of buttons including Top, Next, Prev, and Bottom. It also contains the DataChecker class to check for conflicts during navigation between records. _datanav.vcx
Data Session Manager Manages and controls updates for current data sessions in all forms or form sets. _app.vcx
Data Validation Traps buffered data conflicts. _datanav.vcx
Data Navigation Object Nonvisual object used in conjunction with navigation controls to navigate records in a view or table. (When you use any form of navigation buttons, you must use this object.) _table.vcx
_dbf2html Visual FoxPro cursor (.DBF) to HTML. It contains hooks to control scope, visual layout, and HTML generation. _internet.vcx
Distinct Values Combo Fills a combo box with unique values in the Controlsource field. _dataquery.vcx
Error Object Generic error handler used with the Application Wizard-generated application framework. _app.vcx
Field Mover Displays a dialog box that lets you move fields from one list to another. One list box displays field values from the current data source. _app.vcx
File Version Fetches file version resource and stores it in an array. _utility.vcx
Filter Dialog Box Button Button control that displays the Filter dialog box. _table2.vcx
Filter Dialog Box Displays a dialog box that lets you create or modify a data filter for a designated field. _table.vcx
Filter Expression Dialog Box Displays a dialog box that lets you create or modify a complex data filter expression. _table.vcx
Find Button Displays a Button control that uses the Find object to locate a record based on designated criteria. _table.vcx
Find Dialog Box Displays a dialog box that searches for text in a table or view. Uses other find objects. _table.vcx
Find Files/Text Wraps the FILER.DLL COM object to search for files. This object is a stripped-down version of the FILER form. _utility.vcx
Find Object Creates a nonvisible object that locates a record based on specified criteria. The class also contains an optional Find Next method. It is used with the other Find controls to perform search operations. _table.vcx
Find (FindNext) Buttons Control containing Find and FindNext buttons. It uses the Find object to search for text in tables or views. _utility.vcx
Font Combobox Provides a combo boxcontrol that listsavailable fonts. The tbrEditing and RTFControls classes of _format.scx use this control. _format.vcx
Font Size Combobox Provides a combo box control that lists available font sizes. The tbrEditing and RTFControls classes of format.scx use this control. _format.vcx
Format Toolbar Provides a toolbar that you use to specify text font formatting in the active control _format.vcx
FRX->HTML Converts a Visual FoxPro report (.FRX) to HTML. Contains properties to control the scope and HTML layout. _internet.vcx
GetFile and Directory Provides a dialog box that you use to choose a folder and file. _controls.vcx
GoTo Dialog Box Button Provides a button that displays the GoTo dialog. _table2.vcx
GoTo Dialog Box Provides a dialog that prompts you for a record number to reposition the view or table. _table.vcx
Graph By Record Provides a container object that draws a graph at a record level. It also contains record navigation buttons. _utility.vcx
Graph Object Generates a chart by automating MS Graph. It uses the Graph Wizard engine. autgraph.vcx
Help Button Provides a button control that displays a Help topic. It searches for help associated with the specified HelpContextID property. _miscbtns.vcx
Hyperlink Button Provides a button control used to perform a Hyperlink object operation, which launches a Web browser. _hyperlink.vcx
Hyperlink Image Provides a Hyperlink object-type image control that you can click on to launch a Web browser. _hyperlink.vcx
Hyperlink Label Provides a Hyperlink object-type label control that you can click on to launch a Web browser. _hyperlink.vcx
Item Locator Provides a File Locator dialog box that lets you locate a file that your application needs but cannot find. _dialogs.vcx
INI Access Provides a set of Registry functions to let you access file settings from an .INI file. Registry.vcx
Keywords Dialog Box Provides a dialog box that displays a specified keyword list similar to the Gallery keyword dialog. _dialogs.vcx
Launch Button Provides a button control that you use to launch an application. It uses the RUN command. _miscbtns.vcx
Locate Button Provides a button that displays a dialog to locate a record based on the contents of a field. It operates like the LOCATE command. _table2.vcx
Lookup Combobox Provides a combo box control that is filled with field values. You use this control to let your users look up data values. _dataquery.vcx
Mail Merge Object Provides a Custom class that uses the Mail Merge Wizard engine to generate a Microsoft Word Mail Merge document. Mailmerge.vcx
MessageBox Handler Provides a wrapper around the MessageBox function to simplify custom message display operations. _dialogs.vcx
Mover Provides a simplified mover list box with Move and Remove buttons. See "Super Mover" later in this table. _movers.vcx
MouseOver Effects Provide the functionality to highlight a control when the mouse is dragged over it. _ui.vcx
Navigation Shortcut Menu Provides a shortcut menu that contains navigation, sorting, filtering, and finding options. This shortcut menu displays when the user right-clicks on a form. _table2.vcx
Navigation Toolbar Provides data navigation buttons and other controls in a toolbar. _table2.vcx
Object State Provides a control that determines the state of an object. You can use it to automatically or explicitly save, restore, or set the properties of an object. _app.vcx
ODBC Registry Provides a set of functions you can use to retrieve ODBC-specific Registry information. For example, you can retrieve the driver names and data sources. registry.vcx
Offline Switch Provides a set of control buttons you can use with remote views to toggle between the use of online and offline data. _dataquery.vcx
OK Button Provides a button control used to release a form and update any modified buffered data. _miscbtns.vcx
Output Object A nonvisual object that provides report source and destination options from reports, labels, or a data source. _reports.vcx
Output Control Provides a control that prompts for a report output option. If you use it in conjunction with the Report object, you can also select which report to print. _reports.vcx
Output Dialog Box Provides a dialog box that prompts you for a reporting output option by using the Report object. _reports.vcx
Password Dialog Box Provides a dialog box that prompts the user for a password. _dialogs.vcx
Pivot Table Generates an Excel pivot table based on FoxPro data. It uses the PivotTable Wizard engine. _Pivtable.vcx
Preview Report Provides a button used to run a report preview using the Output dialog. _miscbtns.vcx
QBF Provides a button set used to perform a Query-By-Form. _dataquery.vcx
Registry Access Provides a nonvisible Custom class that you can use to retrieve information from the Windows Registry. registry.vcx
Resize Object Provides a nonvisible class that causes objects that you drag and drop in a form to resize and move with the Resize event of the form. _controls.vcx
RTF Controls Provide controls for applying font formatting to RTF text in an active control. _format.vcx
Run Form Button Provides a button that you use to launch a form. _miscbtns.vcx
SCX->HTML Converts a Visual FoxPro form (.scx) to HTML. Contains properties to control the scope, visual layout, and HTML layout. _internet.vcx
SendMail Buttons Provide a button that uses the MAPI ActiveX control to send mail messages from a form. You can use it to send the current record to your mail application. _miscbtns.vcx
Shell Execute Provides an object that you can use to launch an application or document from the current application. _environ.vcx
Shortcut Menu Class Provides an object that you can use to dynamically create a shortcut menu. _menu.vcx
Simple Edit Buttons Provide a set of Picture buttons consisting of the Add, Edit, Delete, Duplicate, Save, and Cancel buttons. You use these buttons to operate on a simple table or view. This is the same button set used on the Form Wizard. Wizbtns.vcx
Simple Picture Navigation Buttons Provide simple Next and Previous picture buttons used to navigate through a table or view. _table.vcx
Simple Navigation Buttons Provide Next and Previous navigation buttons. _table.vcx
Sort Dialog Box Button Provides a button that you use to display a Sort dialog box. _table2.vcx
Sort Dialog Box Provides a dialog box you can use to perform an ascending or descending data sort on a specified field. _table2.vcx
Sort Object Provides a nonvisible object you can use to sort a data source. _table2.vcx
Sort Selector Provides a set of buttons used to perform an ascending or descending sort. _table2.vcx
Sort Mover Provides an object that is a subclass of the mover list box class. You use it to automatically handle data sorting. _movers.vcx
Sound Player Provides the functionality to load and play a sound file using the Media Control Interface. _multimedia.vcx
Splash Screen Provides a simple splash screen for simple applications. _dialogs.vcx
SQL Pass Through Provides a method to execute a SQL Pass Through operation. You can also execute stored procedures on a remote database such as Microsoft SQL Server. _dataquery.vcx
Stop Watch Provides a stop watch-type control that displays time in the form hh:mm:ss. _datetime.vcx
String Library Provides a nonvisible control that removes carriage returns and line feeds from a character string. You just call the object's TrimCRLF method and pass it the string and indicators specifying whether to trim the left and right sides. _utility.vcx
Super Mover Provides a mover list box with Move, Remove, MoveAll, and Removal buttons. _movers.vcx
System Toolbar Provides a class that manages the placement of the system toolbars. You can use this class in your application to add and remove the Visual FoxPro system toolbars. _app.vcx
Table Mover Provides a list box class that automatically loads tables and fields from the current data source. It is a subclass of the Field Mover list box class. _movers.vcx
Text Preview Provides a simple text editor in a dialog box. It contains buttons to format text, save text, and close the dialog.  
Thermometer Provides a standard thermometer control. _controls.vcx
Trace Aware Timer Provides a nonvisible control that controls the opening of the trace window and handles debugging timing as required. _app.vcx
Type Library Provides a class containing the FOXTLIB ActiveX control that retrieves type information from type libraries. It can create a text file with type library output. _utility.vcx
URL Combo Provides a combo box for entering an Internet Web URL. It launches Internet Explorer to navigate to the specified Web site. _internet.vcx
URL Open Dialog Box Displays a dialog box with a drop-down list that stores the URL history list. _internet.vcx
VCR Buttons Provides a set of record navigation buttons including Top, Next, Previous, and Bottom. _table.vcx
VCR Picture Navigation Buttons Provide a set of record navigation buttons including VCR-type picture buttons for Top, Next, Previous, and Bottom. _table2.vcx
Video Player Provides functionality to load and play a video file. It uses the Media Control Interface (MCI) to display the video file. _multimedia.vcx
Web Browser Control Provides a subclass of Microsoft Internet Explorer that is added to a Visual FoxPro form. This subclass also provides hooks to IE4 so that Visual FoxPro can have full control over the browser's operation. _webview.vcx
Window Handler Provides a nonvisible control that enables window management operations such as tiling and cascading. _ui.vcx

How to Use the Visual FoxPro Foundation Classes

Most of the FoxPro Foundation Classes are added directly to a form and require little or no coding. Classes with a base class of Form, Formset, or Toolbars can be added only to a project and run programmatically.

The simplest way to add a foundation class to your application is to drag and drop it from the Component Gallery to your form or Project Manager. You can also drag and drop a class from the Class Browser, Project Manager, and the Forms Control toolbar to your form. Although the process is not described until Chapter 19, "The Visual FoxPro Component Gallery," using foundation classes is exceptionally easy. All you have to do to run them is to select Tools, Component, and the Component Gallery is displayed as shown in Figure 18.1. Notice that the foundation classes are grouped into 14 categories. When you click on one of the categories, the corresponding classes appear in the right panel. You can click on one of the classes and a brief description appears in the bottom panel. If you want more information about a class, right-click on the class and choose Help from the shortcut menu.

Another advantage of using the Component Gallery as a source of foundation classes for forms and projects is that it regulates which classes can be transferred to a form or project. For example, you do not have to be concerned if the base class for a foundation class is a Form class when you attempt to insert it in a form. In addition, the Component Gallery provides an option of creating a subclass when foundation classes are inserted in a project.

Adding Visual FoxPro Foundation Classes to a Form Using the Component Gallery

Now you can build an application that illustrates how you can use the Component Gallery to add a foundation class to a form. You will use the Data Edit Buttons foundation class that provides navigation and editing support for the application. This control appeared in the right panel of Figure 18.1. Follow these steps to build the application:

Figure 18.1 : The Component Gallery showing the list of Foundation Classes categories in the left panel and the Button controls in the right panel.

  1. Create a project called EmployeeList.
  2. Highlight the Form folder in Project Manager, and click on New to create a new form. Name it Employee.scx.
  3. Choose View, Data Environment.
  4. Right-click on the Data Environment dialog box, and choose a table. In this case, choose the EMPLOYEE.DBF table.
  5. Add a grid to the form. Use the Grid Builder to define the fields and headings. To use the Builder, right-click on the form, and choose the Builder item from the shortcut menu.
  6. Set the form's Caption property to Employee List.
  7. Drag the Data Edit Buttons control to the form, as shown in Figure 18.2.
    Figure 18.2 : The Data Edit Buttons class is dragged from the Component Gallery and dropped on the form.
  8. Add the following three lines of code to the Refresh event of the Data Edit Buttons control:
         PICBTNS::Refresh
    
         NODEFAULT
    
         THISFORM.Grid1.SetFocus
    
    
  9. Close the Form Designer, and name the form Employee.
  10. Click the Project Manager Build button to compile and build the application.

Now you can execute the new Employee List application. The results are shown in Figure 18.3. The Data Edit Buttons object consists of 10 buttons: Top, Previous, Next, Bottom, Find, Print, Add, Delete, Edit, and Save. By adding a single control, you created a complete application that maintains the Employee table. You had to write three lines of code because the grid did not show the repositioned record pointer. The lines of code set the focus to the grid after the Data Edit Buttons object refreshes its display state. The NODEFAULT keyword suppresses the call to the default Refresh event because you already called it explicitly.

Figure 18.3 : The Employee List form application using the Data Edit Buttons object from the Visual FoxPro Foundation Classes.

You can also add a class to a form or a project by right-clicking on the class in the Component Gallery and choosing the Add to Form option from the shortcut menu, as shown in Figure 18.4. In this example, the ActiveX calendar is added to the Employee List form.

Figure 18.4 : The Component Gallery with its shortcut menu displayed.

Follow these steps to add a calendar to the Employee List form:

  1. Open the Form Designer to modify the Employee List form. Then open the Component Gallery.
  2. Right-click on the ActiveX Calendar class in the Component Gallery. Next, choose the Add to Form shortcut menu item.
  3. As soon as the calendar object is placed on the form, the _OLECalendar1 Builder appears. Enter DateHired in the Field Name to Bind Calendar edit box, as shown in Figure 18.5.
    Figure 18.5 : The ActiveX Calendar Class Builder with the bound data field showing.
  4. Rearrange and resize controls on the Employee List form as appropriate.
  5. Add the following line of code to the Grid1.AfterRowColChange event:
   THISFORM._OLECalendar.RefreshDisplay

Each time the grid is moved to a new row or column, the Grid's AfterRowColChange event fires. At that time, the ActiveX Calendar's RefreshDisplay is called to update the calendar date and refresh the calendar display. The calendar date is updated from the bound DateHired field in the Employee table.

Now run the Employee List form and observe the results, as shown in Figure 18.6. Notice that when you move to a new row, the date in the calendar is the same as the grid's Date Hired field. This enhancement to the Employee List code took only one additional line of code. Imagine how many lines of code you would have to write to duplicate this example if you had to write it all from scratch.

Figure 18.6 : Results of running Employee List with a calendar added.

Adding Visual FoxPro Foundation Classes to a Form Using the Form Control Toolbar

You can add Visual FoxPro Foundation Classes from the Form Control toolbar, but first you have to add the visual class library file (.VCX) for your desired foundation class to your Form Control toolbar. The classes are all stored in the \VFP98\ffc directory. Table 18.2 lists the foundation categories and their associated visual class libraries.

Table 18.2  FoxPro Foundation Class Categories and Associated Visual Class
Libraries

Category

Visual Class Library

Application _app.vcx
Automation autgraph.vcx
Buttons _miscbtns.vcx
Data Navigation _datanav.vcx
Data Query _dataquery.vcx
Date/Time _datetime.vcx
Dialogs _dialogs.vcx
Internet _hyperlink.vcx
Menus _table2.vcx
Multimedia _multimedia.vcx
Output _reports.vcx
Text Formatting _format.vcx
User Controls _ui.vcx
Utilities registry.vcx
NOTE
Notice that most of the names of the Visual FoxPro Foundation Classes begin with an underscore (_). It is added by convention to denote which foundation classes are derived from classes in the visual class library, _base.vcx. Specifically, foundation classes that begin with an underscore are subclassed from a _base.vcx class.
What is important is that you can make enhancements to the classes in _base.vcx. All other Visual FoxPro Foundation Classes that have their name preceded with an underscore can benefit from the enhancements because they are derived from _base.vcx classes.
Follow these steps to add the classes to your Form Control toolbar from the options menu:
  1. Choose Tools, Options. Then click on the Controls tab. Next, choose the Add option, and a File Open dialog box appears.
  2. Choose one of the visual class libraries from Table 18.2 from the \VFP98\ffc directory.
  3. Click the Save as Default button if you want the control to remain on the Form Control toolbar for subsequent Visual FoxPro sessions.
  4. Click the OK button to exit the options menu.

If you want the foundation class to remain on the Form Control toolbar only for the current Visual FoxPro session, click on the View Classes icon on the Form Control toolbar, and choose the Add option. A File Open dialog box then appears. Then choose one of the visual class libraries in Table 18.2.

After you place the desired foundation classes on the Form Control toolbar, you can drag and drop a foundation class from the Form Control toolbar to your form. For example, I added the Date/Time category foundation classes to the Form Control toolbar, as shown in Figure 18.7. You can drag and drop the Clock class (_clock) to the form (see Figure 18.7).

Figure 18.7 : Adding the Clock foundation class to a form on the Form Control toolbar.

If you run the enhanced Employee List form, it will appear as shown in Figure 18.8, complete with the date and time displayed, compliments of the clock object.

Figure 18.8 : The Employee List form displaying the date and time, compliments of the clock object.

Adding Visual FoxPro Foundation Classes to a Project

Just as you can add foundation classes to a form, you can also add them to a project. Usually, you add foundation classes to a form. However, foundation classes with a base class of Form, FormSet, or Toolbar can be added only to a project. For example, the About dialog class or a Splash Screen class must be added to a project. Then you add code to activate them.

The easiest and best way to add a foundation class to a project is to drag a class from the Component Gallery to the project or choose the Component Gallery shortcut menu's Add to Project item. Of course, you can use other ways. You can drag the visual class library file (.VCX) from the Windows Explorer to the project. Another way is to choose the Project Manager Add button to add the visual class library file to the project.

When you add the foundation class to the project using the Component Gallery, the Add Class to Project dialog box is displayed, as shown in Figure 18.9.

Figure 18.9 : The Add Class to Project dialog box is displayed when you add a foundation class to a project using the Component Gallery.

In this dialog box, you have three options. You can simply add the class to the project, create a new class derived from the selected class, or create a new form containing the class.

If you choose the Add Class to Project option, the visual class library file is added to the project. You should choose this option when you want to execute the foundation class programmatically, and you don't plan to enhance the functionality of the class.

If you want to change the foundation class's functionality, you can choose the Create New Class from Selected Class option. You are prompted to enter the name of the new class that is derived from the selected class. Then you can add code to modify the behavior of the class.

If you want to modify the appearance of the selected class's form, you can choose the Create New Form from Selected Class option. In this case, you are prompted to enter the name of the new form. Then you can modify the new form as you please.

Suppose you want to add an About box to the Employee List form. Here are the steps:

  1. Open the EmployeeList project.
  2. Drag the About box class from the Component Gallery to the EmployeeList project.
  3. Choose the Create New Form from Selected Class option in the Add Class to Project dialog box.
  4. When you are asked to name the new form, name it About.
  5. After the Form Designer appears showing the new About form, change items on the form as appropriate. Figure 18.10 shows it with a Logo (.GIF) file added and some changes to labels and the Caption property.
    Figure 18.10: The Visual FoxPro Form Designer with the new About form open.
  6. Modify the Employee List form, and add a command button to the form with the Caption property set to About.
  7. Add the following line of code to the Click event for the About button:
    DO FORM ABOUT
    
    
  8. Build and run the application. Figure 18.11 shows the new About command button on the Employee List form and the new About dialog box that results when you click this button.

Figure 18.11: The Employee List and About forms at runtime.

The next example illustrates how to add a splash screen to an application using the Splash Screen dialog class. The class will be executed programmatically to show the splash screen.

I added a program named EmployeeList.Prg to the EmployeeList project to display the splash screen before the Employee List dialog box activates. You don't need to add the Splash Screen class to the project because the _dialogs.vcx visual class library was added to the project when you added the About class. Follow these steps to create this example:

  1. Open the EmployeeList project.
  2. Click on the Programs folder, and click on the New button. A text edit window appears.
  3. Enter the following code into the text window:
         oSplash = NewObject("_splash","_dialogs")
    
         IF VARTYPE(oSplash) = "O"
    
             oSplash.Picture = HOME()+"hlpglobe.gif"
    
             oSplash.nDuration= 5   && Set the duration to 5 seconds
    
             oSplash.Show()
    
         ENDIF
    
         DO FORM Employee
    
    
  4. Save the program, and name it EmployeeList.prg.
  5. Highlight the EmployeeList.prg program in the project, and choose Project, Set Main to make this program the main program instead of the Employee form.
  6. Rebuild and run the application. The resulting splash screen is shown in Figure 18.12.

Figure 18.12: The EmployeeList application's splash screen.

You use the NewObject() function in this application to open the Splash Screen class in the _dialogs visual class library and save the object variable oSplash. Next, you assign new values to the splash screen's Picture and nDuration properties. Finally, you launch the splash screen by using the Show() method.

More Examples of Using the FoxPro Foundation Classes

To help you learn how to use the FoxPro Foundation Classes and other aspects of Visual FoxPro, Microsoft has supplied the Solutions application in the Samples directory. When you execute the Solutions application, a dialog box appears, as shown in Figure 18.13. You can choose an application from the outline list and either run it or look at the code to find out how it works.

Figure 18.13: The Solutions sample application dialog box.


© Copyright, Sams Publishing. All rights reserved.