Partager via


Example of Manipulating Objects

The following example sets properties and calls event code from various objects within a form set. The example includes two forms, frmLeft and frmRight, in a formset.

Sample form set in the Form Designer

FoxProLeftFormTextInput screenshot

The two check boxes and the command button on frmLeft have event code associated with them. The name of the text box on frmLeft is txtInput.

Event Code for Objects in LeftForm

Object

Event

Code

chkItalic

Click

THISFORM.txtInput.FontItalic = ;

THIS.Value

chkBold

Click

THIS.txtInput.FontBold = THIS.Value

cmdClear

Click

THISFORM.txtInput.Value = ""

THISFORM.txtInput.FontBold = .F.

THISFORM.txtInput.FontItalic = .F.

THISFORM.chkItalic.Value = .F.

THISFORM.chkBold.Value = .F.

Setting a Property of Another Control on the Same Form

You can set the properties of one control from within the event code of another by using the THISFORM keyword or the Parent property. The following two commands are executed when a user initially clicks on the Italic and the Bold check boxes, setting the appropriate text box properties:

THISFORM.txtInput.FontItalic = .T.
THIS.Parent.txtInput.FontBold = .T.

In this case, THISFORM and THIS.Parent can be used interchangeably.

Sample form set at run time

FoxProLeftFormUserInput screenshot

The code in the click event for cmdClear uses THISFORM to reset the values of the other controls on the form.

Setting Another Form's Properties

You can also set properties of one form from another. Form2 contains five command buttons. The first button on the form has this code in its Click event:

THISFORMSET.frmLeft.Caption = ;
 ALLTRIM(ThisFormSet.frmLeft.txtInput.Value)

Notice that the form set and the form need to be referenced when setting properties from within a different form.

User clicks "Change Left Form Caption" command button on Right Form

FoxPro Object Form

The click event code of the second command button on frmRight demonstrates setting a property of a form from within an object on the form:

THISFORM.Caption = ;
 ALLTRIM(ThisFormSet.frmLeft.txtInput.Value)

If the user chooses this button, the caption of frmRight changes to the value in the text box on frmLeft.

Accessing Objects on Different Forms

The following code in the Click event of the Change Bold Setting command button changes the value of the Bold check box on frmLeft and calls the event code associated with this control.

THISFORMSET.frmLeft.chkBold.Value = ;
   NOT THISFORMSET.frmLeft.chkBold.Value
THISFORMSET.frmLeft.chkBold.InteractiveChange

The last line of the example calls the InteractiveChange event of chkBold. You could also call this procedure with the following command:

THISFORMSET.frmForm1.chkBold.InteractiveChange( )

If this procedure call is omitted, the value of the check box changes, but the FontBold property of the text box is never changed.

User clicks "Change Bold Setting" command button on Right Form

FoxPro Object Form Settings

Checking Properties and Calling Method Code of Another Form

The following code in the Click event of the Hide Left Form command button hides or shows frmLeft, depending on the value of the Visible property, and changes the button caption as appropriate:

IF ThisFormSet.frmLeft.Visible
   ThisFormSet.frmLeft.Hide
   THIS.Caption = "Show Left Form"
ELSE
   ThisFormSet.frmLeft.Show
   THIS.Caption = "Hide Left Form"
ENDIF

Notice that the THIS keyword is used within event code of a control to reference properties of the control.

User clicks Hide Left Form command button on Right Form

FoxProRightForm screenshot

The following command in the Click event of the Quit command button releases the form set, causing both forms to close:

RELEASE ThisFormSet

See Also

Tasks

How to: Set Properties at Run Time

How to: Hide a Form

How to: Pass Parameters to a Form

How to: Save a Form as HTML

Other Resources

Working with Forms

Manipulating Objects

Creating Forms