Page 493
Figure 18.7.
Testing the form that
displays a calculated
value.
A major feature of Power Objects is its support for classes.
Figure 18.8.
Creating a new class.
Page 494
Figure 18.9. Figure 18.10. Page 495
Let's change some of the properties of the pushbutton. Select the pushbutton,
right-click Property Sheet, and set these properties:
Close the property sheet. Before you add an instance of the class to an existing form,
you'll want to give the new class a name. Therefore, select the class by clicking anywhere within
the class's defining border except the pushbutton and then right-clicking Property Sheet. Set
the Name property for the class to MyFirstClass, and exit the property sheet. Click the Save
button on the toolbar to save the changes to the class. Press Ctrl+w to close the class.
If you look at the Main window, you should now see the class that you've
createdMyFirstClassin the collection of objects that belong to the Flugle application (see
Figure 18.11).
Figure 18.11. You're going to add an instance of MyFirstClass to
MyForm. To do this, select the icon next to
MyFirstClass, and hold down the left mouse button. Drag the icon onto the icon
for MyForm; you should see a + near the pointer. Release the mouse button. Power Objects
will display MyForm with the class instance in the upper-left corner (see Figure 18.12).
Page 496 Figure 18.12.
You can reposition MyFirstClass on MyForm by selecting it and dragging it to the
right, revealing the two text fields that you previously created, XPos and YPos. Try running the form by selecting Run | Run Form from the menu. Try clicking on Close. Did you expect
anything to happen? Nothing should because you haven't provided any method code for the
button's Click method. Click Stop to return to Design mode.
To make the pushbutton close the form in which it is placed, open
MyFirstClass, select the pushbutton, and right-click Property Sheet. Scroll down to the
Click method, and click the method title. As shown in Figure 18.13, enter two lines of Oracle Basic:
The first line invokes the MSGBOX function. The argument to
MSGBOX is a concatenation of Closing with another item. The second item uses the
GetTopContainer to obtain an object reference to the top containerform, report, or classin which the object is
contained. GetTopContainer.Name returns the name of the top object in which the pushbutton
is contained. The net effect of this line is to issue a message box that will display the name
of whatever form contains an instance of
MyFirstClass. The second line will close the form
that contains this pushbutton.
To test MyForm again, double-click MyForm on the Main Window. Click on Close. As you
can see in Figure 18.14, a message box appears, informing you that the button is about to
close MyForm. Click OK on the message box. Click Stop to return to Design mode.
Page 497 Figure 18.13. Figure 18.14. You've now seen a simple example on the use of classes. By changing the method code
for an object in the class, you affect the behavior of other objects that contain an instance of
the modified class.
Let's look at the effect of changing the property of an object in a class. As you may have
noticed in MyForm, there is a border surrounding the Close button. The border is defined in the
class. Invoke MyFirstClass by double-clicking its icon in the Main Window. Select the
MyFirstClass window, and right-click Property Sheet. Scroll down to the
HasBorder property, and click it so that it changes to
False. Exit the Property Sheet.
Page 498 Invoke MyForm from the Main Window by double-clicking its icon. Notice that there is
no longer a border surrounding the Close button.
In the last example, you changed the HasBorder property for
MyFirstClass by setting it to False; as a result, an instance of
MyFirstClass on another form inherits the property of
having no border. However, you can override a property or method in a class instance. For
example, invoke MyForm and select the instance of
MyFirstClass that you placed on it. Right-click Property Sheet. The
HasBorder property is set to False, the value that you set for the
class property. However, you can override that property for this instance of
MyFirstClass by clicking False; the value will become
True. The diamond to the left of the property name
will become black, signifying that the inherited property has been overridden (see Figure 18.15).
Figure 18.15.
Adding a pushbutton
to the new class.
Reducing the size of
the class.
Label Close
Name btnClose
Adding an Instance of a Class to a Form
Main Window
displays
MyFirstClass class.
MyForm contains an
instance of
MyFirstClass.
Adding Method Code to the Class
MSGBOX("Closing " + GetTopContainer.Name)
GetTopContainer.CloseWindow()
Specifying method
code for the Click
method in btnClose.
Testing the Click
method in btnClose.
Changing a Property in a Class Object
Overriding an Inherited Class Property
Overriding an
inherited class
property.
NOTE
If you look at the PositionX and PositionY properties for the instance of MyFirstClass on MyForm, you'll notice that the black diamond indicates that their inherited values have been overridden. This happened automatically when you dragged MyFirstClass to the right on the MyForm.