Condividi tramite


Container Hierarchy Object Referencing

The class hierarchy and the container are two separate entities. Visual FoxPro looks for event code up through the class hierarchy, whereas objects are referenced in the container hierarchy. To manipulate an object, you need to identify it in relation to the container hierarchy. For example, to manipulate a control on a form in a form set, you need to reference the form set, the form, and then the control. For an explanation of class hierarchies, see Calling Event Code up the Class Hierarchy.

You can compare the referencing of an object within its container hierarchy to giving Visual FoxPro an address to your object. When you describe the location of a house to someone outside your immediate frame of reference, you need to indicate the country, the state or region, the city, the street, or just the street number of the house, depending on how far they are from you. Otherwise, there could be some confusion.

The following illustration shows a possible container-nesting situation.

Nested containers

To disable the control in the grid column, you need to provide the following address:

Formset.Form.PageFrame.Page.;
 Grid.Column.Control.Enabled = .F.

The ActiveForm property of the application object (_VFP) allows you to manipulate the active form even if you don't know the name of the form. For example, the following line of code changes the background color of the active form, no matter what form set it belongs to:

_VFP.ActiveForm.BackColor = RGB(255,255,255)

Similarly, the ActiveControl property allows you to manipulate the active control on the active form. For example, the following expression entered in the Watch window displays the name of the active control on a form as you interactively choose the various controls:

_VFP.ActiveForm.ActiveControl.Name

Relative Referencing

When you are referencing objects from within the container hierarchy (for example, in the Click event of a command button on a form in a form set), you can use some shortcuts to identify the object you want to manipulate. The following table lists properties or keywords that make it easier to reference an object from within the object hierarchy:

Property or keyword Reference
Parent The immediate container of the object.
THIS The object.
THISFORM The form that contains the object.
THISFORMSET The form set that contains the object.

Note   You can use THIS, THISFORM, and THISFORMSET only in method or event code.

The following table provides examples of using THISFORMSET, THISFORM, THIS, and Parent to set object properties:

Command Where to include the command
THISFORMSET.frm1.cmd1.Caption = "OK"
In the event or method code of any controls on any form in the form set.
THISFORM.cmd1.Caption = "OK"
In the event or method code of any control on the same form that cmd1 is on.
THIS.Caption = "OK"
In the event or method code of the control whose caption you want to change.
THIS.Parent.BackColor = RGB(192,0,0)
In the event or method code of a control on a form. The command changes the background color of the form to dark red.

See Also

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