Using the Class Designer
When you specify what class your new class is based on and the library to store the class in, the Class Designer opens.
Class Designer
The Class Designer provides the same interface that the Form Designer does, allowing you to see and edit the properties of your class in the Properties window. Code editing windows allow you to write code to be executed when events occur or methods are called.
Adding Objects to a Control or Container Class
If you base the new class on the control or container class, you can add controls to it the same way you add controls in the Form Designer: choose the control button on the Form Controls toolbar and drag to size in the Class Designer.
No matter what type of class you base the new class on, you can set properties and write method code. You can also create new properties and methods for the class.
Adding Properties and Methods to a Class
You can add as many new properties and methods to the new class as you want. Properties hold values; methods hold procedural code to be run when you call the method.
Creating New Properties and Methods
When you create new properties and methods for classes, the properties and methods are scoped to the class, not to individual components in the class.
To add a new property to a class
From the Class menu, choose New Property.
1.
In the New Property dialog box, type the name of the property.
2.
Specify the visibility: Public, Protected, or Hidden.
A Public property can be accessed anywhere in your application. Protected and Hidden properties and methods are 3.
discussed in “Protecting and Hiding Class Members” later in this chapter.
New Property dialog box
Choose Add.
You can also include a description of the property that will be displayed at the bottom of the Properties window in the Class Designer and in the Form Designer when the control is added to a form.
4.
Troubleshooting When you add a property to a class that can be set by a user of the class, the user could enter an invalid setting for your property that could cause run-time errors. You need to explicitly document the valid settings for the property. If your property can be set to 0, 1, or 2, for example, say so in the Description box of the New Property dialog box. You might also want to verify the value of the property in code that references it.
To create an array property
In the Name box of the New Property dialog box, specify the name, size, and dimensions of the array.
For example, to create an array property named myarray with ten rows and two columns, type the following in the Name box:
myarray[10,2]
●
The array property is read-only at design time and is displayed in the Properties window in italics. The array property can be managed and redimensioned at run time. For an example of using an array property, see “Managing Multiple Instances of a Form” in Chapter 9, Creating Forms.
To add a new method to a class
From the Class menu, choose New Method.
1.
In the New Method dialog box, type the name of the method.
2.
Specify the visibility: Public, Protected, or Hidden.
3.
Select the Access check box to create an Access method, select the Assign check box to create an Assign method, or select both check boxes to create Access and Assign methods.
4.
Access and Assign methods let you execute code when the value of a property is queried or when you attempt to change the property’s value.
The code in an Access method is executed when the value of a property is queried, typically by using the property in an object reference, storing the value of the property to a variable, or displaying the value of property with a question mark (?).
The code in an Assign method is executed when you attempt to change the value of a property, typically by using the STORE or
= command to assign a new value to the property.
For more information about Access and Assign methods, see Access and Assign Methods.
You can also include a description of the method.
Protecting and Hiding Class Members
Properties and methods in a class definition are Public by default: code in other classes or procedures can set the properties or call the methods. Properties and methods that you designate as Protected can be accessed only by other methods in the class definition or in subclasses of the class. Properties and methods designated as Hidden can be accessed only by other members in the class definition. Subclasses of the class cannot “see” or reference hidden members.
To ensure correct functioning in some classes, you need to prevent users from programmatically changing the properties or calling the method from outside the class.
The following example illustrates using protected properties and methods in a class.
The stopwatch class included in Samples.vcx, in the Visual Studio …\Samples\Vfp98\Classes directory, includes a timer and five labels to display the elapsed time:
The stopwatch class in Samples.vcx
The Stopwatch class contains labels and a timer.
Property Settings for the Stopwatch Class
Control Property Setting
lblSeconds Caption 00
lblColon1 Caption :
lblMinutes Caption 00
lblColon2 Caption :
lblHours Caption 00
tmrSWatch Interval 1000
This class also has three protected properties, nSec, nMin, and nHour, and one protected method, UpdateDisplay. The other three custom methods in the class, Start, Stop, and Reset, are not protected.
Tip Choose Class Info on the Class menu to see the visibility of all properties and methods of a class.
The protected properties are used in internal calculations in the UpdateDisplay method and the Timer event. The UpdateDisplay method sets the captions of the labels to reflect the elapsed time.
The UpdateDisplay Method
Code Comments
cSecDisplay = ALLTRIM(STR(THIS.nSec)) cMinDisplay = ALLTRIM(STR(THIS.nMin)) cHourDisplay = ALLTRIM(STR(THIS.nHour))
Convert the numeric properties to Character type for display in the label captions.
THIS.lblSeconds.Caption = ; "0", "") + cHourDisplay
Set the label captions, retaining the leading 0 if the value of the numeric property is less than 10.
The following table lists the code in the tmrSWatch.Timer event:
The Timer Event
Increment the nSec property every time the timer event fires: every second.
If nSec has reached 60, reset it to 0 and increment the nMin property.
IF THIS.Parent.nMin = 60
If nMin has reached 60, reset it to 0 and increment the nHour property.
Call the UpdateDisplay method when the new property values are set.
The stopwatch class has three methods that are not protected: Start, Stop, and Reset. A user can call these methods directly to control the stopwatch.
The Start method contains the following line of code:
THIS.tmrSWatch.Enabled = .T.
The Stop method contains the following line of code:
THIS.tmrSWatch.Enabled = .F.
The Reset method sets the protected properties to zero and calls the protected method:
THIS.nSec = 0 THIS.nMin = 0 THIS.nHour = 0 THIS.UpdateDisplay
The user cannot directly set these properties or call this method, but code in the Reset method can.
Specifying the Default Value for a Property
When you create a new property, the default setting is false (.F.). To specify a different default setting for a property, use the Properties window. In the Other tab, click on your property and set it to the desired value. This will be the initial property setting when the class is added to a form or form set.
You can also set any of the base class properties in the Class Designer. When an object based on the class is added to the form, the object reflects your property settings rather than the Visual FoxPro base class property settings.
Tip If you want to make the default setting of a property an empty string, select the setting in the Property Editing box and press the BACKSPACE key.
Specifying Design Time Appearance
You can specify the toolbar icon and the container icon for your class in the Class Info dialog box.
To set a toolbar icon for a class
In the Class Designer, choose Class Info from the Class menu.
1.
In the Class Info dialog box, type the name and path of the .BMP file in the Toolbar icon box.
Tip The bitmap (.bmp file) for the toolbar icon is 15 by 16 pixels. If the picture is larger or smaller, it is sized to 15 by 16 pixels and might not look the way you want it to.
2.
The toolbar icon you specify is displayed in the Form Controls toolbar when you populate the toolbar with the classes in your class library.
You can also specify the icon to be displayed for the class in the Project Manager and Class Browser by setting the container icon.
To set a container icon for a class
In the Class Designer, choose Class Info from the Class menu.
1.
In the Container icon box, type the name and path of the .bmp file to be displayed on the button in the Form Controls toolbar.
2.
Using Class Library Files
Every visually designed class is stored in a class library with a .vcx file extension.
Creating a Class Library
You can create a class library in one of three ways.
To create a class library
When you create a class, specify a new class library file in the Store In box of the New Class dialog box.
-or-●
Use the CREATE CLASS command, specifying the name of the new class library.
For example, the following statement creates a new class named myclass and a new class library named new_lib:
CREATE CLASS myclass OF new_lib AS CUSTOM
-or-●
Use the CREATE CLASSLIB command.
For example, type the following command in the Command window to create a class library named new_lib:
CREATE CLASSLIB new_lib
●
Copying and Removing Class Library Classes
Once you add a class library to a project, you can easily copy classes from one library to another or simply remove classes from
libraries.
To copy a class from one library to another
Make sure both libraries are in a project (not necessarily the same project).
1.
In the Project Manager, select the Classes tab.
2.
Click the plus sign (+) to the left of the class library that the class is now in.
3.
Drag the class from the original library and drop it in the new library.
Tip For convenience and speed, you might want to keep a class and all the subclasses based on it in one class library. If you have a class that contains elements from many different class libraries, these libraries must all be open, so it will take a little longer to initially load your class at run time and at design time.
4.
To remove a class from a library
Select the class in the Project Manager and choose Remove.
-or-●
Use the REMOVE CLASS command.
●
To change the name of a class in a class library, use the RENAME CLASS command. Remember, however, that when you change the name of a class, forms that contain the class and subclasses in other .vcx files continue to reference the old name and will no longer function correctly.
Visual FoxPro includes a Class Browser to facilitate using and managing classes and class libraries. For more information, see Class Browser window.