Preparation for Class Creation

You want to be able to use classes in many different contexts. Smart planning will enable you to most effectively decide what classes to design and what functionality to include in the class.

Deciding When to Create Classes

You could create a class for every control and every form you might ever use, but this isn't the most effective way to design your applications. You'll likely end up with multiple classes that do much the same thing but must be maintained separately.

Encapsulate Generic Functionality

You can create a control class for generic functionality. For example, command buttons that allow a user to move the record pointer in a table, a button to close a form, and a help button, can all be saved as classes and added to forms any time you want the forms to have this functionality.

You can expose properties and methods on a class so that the user can integrate them into the particular data environment of a form or form set.

Provide a Consistent Application Look and Feel

You can create form set, form, and control classes with a distinctive appearance so that all the components of your application have the same look. For example, you could add graphics and specific color patterns to a form class and use that as a template for all forms you create. You could create a text box class with a distinctive appearance, such as a shadowed effect, and use this class throughout your application any time you want to add a text box.

Deciding What Type of Class to Create

Visual FoxPro allows you to create several different kinds of classes, each with its own characteristics. You specify the type of class you want to create in the New Class dialog box or in the AS clause in the CREATE CLASS command.

The Visual FoxPro Base Classes

You can create subclasses of most of the Visual FoxPro base classes in the Class Designer.

Visual FoxPro Base Classes

ActiveDoc Custom Label PageFrame
CheckBox EditBox Line ProjectHook
Column* Form ListBox Separator
CommandButton FormSet OLEBoundControl Shape
CommandGroup Grid OLEContainerControl Spinner
ComboBox Header* OptionButton* TextBox
Container Hyperlink Object OptionGroup Timer
Control Image Page* ToolBar

* These classes are an integral part of a parent container and cannot be subclassed in the Class Designer.

All Visual FoxPro base classes recognize the following minimum set of events:

Event Description
Init Occurs when the object is created.
Destroy Occurs when the object is released from memory.
Error Occurs whenever an error occurs in event or method procedures of the class.

All Visual FoxPro base classes have the following minimum set of properties:

Property Description
Class What type of class it is.
BaseClass The base class it was derived from, such as Form, Commandbutton, Custom, and so on.
ClassLibrary The class library the class is stored in.
ParentClass The class that the current class was derived from. If the class was derived directly from a Visual FoxPro base class, the ParentClass property is the same as the BaseClass property.

Extending the Visual FoxPro Base Classes

You can subclass these classes to set your own default control properties. For example, if you want the default names of controls you add to forms in your applications to automatically reflect your naming conventions, you can create classes based on the Visual FoxPro base classes to do this. You can create form classes with a customized look or behavior to serve as templates for all the forms you create.

You could also subclass the Visual FoxPro base classes to create controls with encapsulated functionality. If you want a button to release forms when the button is clicked, you can create a class based on the Visual FoxPro command button class, set the caption property to "Quit" and include the following command in the Click event:

THISFORM.Release

You can add this new button to any form in your application.

Creating Controls with Multiple Components

Your subclasses aren't limited to single base classes. You can add multiple controls into a single container class definition. Many of the classes in the Visual FoxPro sample class library fall into this category.

Creating Non-Visual Classes

A class based on the Visual FoxPro custom class doesn't have a run-time visual element. You can create methods and properties for your custom class using the Class Designer environment. For example, you could create a custom class named StrMethods and include a number of methods to manipulate character strings. You could add this class to a form with an edit box and call the methods as needed. If you had a method called WordCount, you could call it when needed:

THISFORM.txtCount.Value = ;
  THISFORM.StrMethods.WordCount(THISFORM.edtText.Value)

Non-visual classes (like the custom control and the timer control) have a visual representation only at design time in the Form Designer. Set the picture property of the custom class to the .bmp file you want displayed in the Form Designer when the custom class is added to a form.

See Also

Object-Oriented Programming | Classes and Objects: The Building Blocks of Applications | Classes in Visual FoxPro | Creating Classes | Modifying a Class Definition | Subclassing a Class Definition | Operating the Class Designer | Class Member Protection and Hiding | Specifying Design-Time Appearance | Creating, Copying, and Removing Class Library Files | Adding Classes to Forms | Default Property Setting Override | Container Hierarchy Object Referencing | Setting Properties | Calling Methods | Event Response