Previous | Table of Contents | Next

Page 493

Figure 18.7.
Testing the form that
displays a calculated
value.

Creating a Class

A major feature of Power Objects is its support for classes.

  1. You can add a class to an application by pressing the New Class button on the toolbar or by selecting Object | New Class from the menu. Figure 18.8 illustrates what the new class looks like—as you would expect, it is initially empty.

Figure 18.8.
Creating a new class.

  1. You're going to add a button to this class. On the Object Palette, select the pushbutton icon and create a button on the class. Power Objects will assign a name to the button, as shown in Figure 18.9.

Page 494

Figure 18.9.
Adding a pushbutton
to the new class.

  1. Reduce the size of the class by grabbing the lower-right corner and dragging to the upper-left, as shown in Figure 18.10.

Figure 18.10.
Reducing the size of
the class.

Page 495

Let's change some of the properties of the pushbutton. Select the pushbutton, right-click Property Sheet, and set these properties:

   Label     Close

   Name     btnClose

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.

Adding an Instance of a Class to a Form

If you look at the Main window, you should now see the class that you've created—MyFirstClass—in the collection of objects that belong to the Flugle application (see Figure 18.11).

Figure 18.11.
Main Window
displays
MyFirstClass class.

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.
MyForm contains an
instance of
MyFirstClass.

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.

Adding Method Code to the Class

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:

MSGBOX("Closing " + GetTopContainer.Name)
GetTopContainer.CloseWindow()

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 container—form, report, or class—in 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.
Specifying method
code for the Click
method in btnClose.

Figure 18.14.
Testing the Click
method in btnClose.

Changing a Property in a Class Object

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.

Overriding an Inherited Class Property

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.
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.

Previous | Table of Contents | Next

Используются технологии uCoz