Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

This, ThisForm, ThisFormSet

The documentation refers to these three as “object references” and we can’t think of anything better. They’re not commands nor functions; neither are they properties, events nor methods. And they’re not exactly variables, either. What they are, in an informal sense, is handles. They let you grab onto objects without knowing the names of the objects.

Usage

This.Property | Method | MemberName
ThisForm.Property | Method | MemberName
ThisFormSet.Property | Method | MemberName

When you’re writing method code for an object, you often need to refer to properties and methods of that object and of other related objects. But you don’t know what the name of the object will be when it’s instantiated. So you often can’t write something like MyWonderfulObject.Caption.

Even when you do know the name (for example, when you’re designing a form that will have only one instance), it’s not a good idea to refer to the object by name. Doing so limits the reusability of the object.

Similarly, in a method of a form or formset, you don’t want to refer to it by name since you may instantiate it more than once, with a different name for each instance.

The three object references are the way to avoid the problem. THIS refers to the current object. When you’re in a method of a control, THIS is the control itself. In a method of a form, THIS is the form. In a method of a formset, THIS is the formset, and so on. The same idea applies to non-visual objects as well. THIS is always the object whose method you’re in.

ThisForm, then, is the form containing the object. In a method of a control, ThisForm refers to the form containing the control. In a method of a form, ThisForm and THIS both refer to the form. Finally, ThisFormSet refers to the form set containing the object whose method you’re in.

Example

oTest=CREATEOBJECT("TestForm")
oTest.SHOW()
READ EVENTS

DEFINE CLASS TestForm AS Form

   ScaleMode = 0  && Foxels just to make it easier
                  && to position things
   ADD OBJECT txtUpdate AS Updater
   ADD OBJECT cmdClear AS ClearButton
   ADD OBJECT cmdQuit AS QuitButton

PROCEDURE Init

   * set up Clear button to clear text box
   This.cmdClear.cWhatToClear="txtUpdate"

   * position things
   This.Height=10
   This.Width=30
   This.AutoCenter=.T.

   This.txtUpdate.Left=5
   This.txtUpdate.Top=2

   This.cmdClear.Left=5
   This.cmdClear.Top=5

   This.cmdQuit.Left=15
   This.cmdQuit.Top=5

ENDPROC

ENDDEFINE

DEFINE CLASS Updater AS Textbox

   Width=20
   Height=1.5

PROCEDURE InteractiveChange
  * change the form caption

   ThisForm.Caption=This.Value

ENDPROC

PROCEDURE ProgrammaticChange
   * revert the form caption

   ThisForm.Caption="TestForm"

ENDPROC

ENDDEFINE

DEFINE CLASS ClearButton AS CommandButton

   Width=7
   Height=1.5
   Caption="Clear"

   * add a custom property
   cWhatToClear=""

PROCEDURE Click
    LOCAL cClearWhat
    cClearWhat=This.cWhatToClear
    ThisFORM.&cClearWhat..Value=""
ENDPROC

ENDDEFINE

DEFINE CLASS QuitButton AS CommandButton

   Caption="Quit"
   Width=7
   Height=1.5

PROCEDURE Click

   CLEAR EVENTS

ENDPROC

PROCEDURE Destroy

   CLEAR EVENTS

ENDPROC
ENDDEFINE

The example includes several class definitions, three of them controls (two buttons and a text box) and one for a form class. In the form’s Init method, we can change properties of the form using THIS.property (for example, THIS.Height=10). But, in the Text box’s InteractiveChange event, to change the form’s caption, we need to use ThisForm.Caption, indicating that FoxPro should look outside the current object (the text box) to see the containing form.

We should point out that this sample does something we never actually do—it uses foxels. We always work in pixels. But for a hand-coded sample like this, foxels make things much easier.

See Also

CreateObject(), Define Class