Partager via


How to: Set Properties at Run Time

The object model in Visual FoxPro makes it possible for you to control form properties at run time.

Referencing Objects in the Object Hierarchy

To manipulate an object in code, you need to identify it in relation to the container hierarchy. At the highest level of the container hierarchy, that is, the form or form set, you need to reference the object variable. Unless you use the NAME clause of the DO FORM Command, the object variable has the same name as the form (.scx) file.

To manipulate properties

  • In code, reference the object variable, the control, and then the property with the dot (.) operator as a separator and using the following format:

    ObjectVariable.[Form.]Control.Property = Setting

The following table lists properties or keywords that make it easier to reference an object in the object hierarchy.

Property or keyword

References

ActiveControl

The control on the currently active form that has the focus.

ActiveForm

The currently active form.

ActivePage

The active page on the currently active form.

Parent

The immediate container of the object

THIS

The object or a procedure or event of the object.

THISFORM

The form that contains the object.

THISFORMSET

The form set that contains the object.

For example, to change the caption of a command button on the form frmCust in a form set stored in Custview.scx, use the following command in a program or in the Command window:

CustView.frmCust.cmdButton1.Caption = "Edit"

Use the THIS, THISFORM, and THISFORMSET keywords to reference objects from within a form. For example, to change the Caption of a command button when the command button is clicked, include the following command in the Click event code for the command button:

THIS.Caption = "Edit"

The following table gives 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 control on any form in the form set except for frm1.

THISFORM.cmd1.Caption = 'OK'

In the event or method code of any control except for cmd1 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.

Setting Properties at Run Time with Expressions

You can also set properties at run time using expressions or functions.

To set properties to expressions at run time

  1. Assign an expression to the property.

    -or-

  2. Assign the result of a user-defined function to the property.

    For example, you could set the caption of a button to be Edit or Save, depending on the value of a variable. Declare the variable in the calling program for your form:

    PUBLIC glEditing
    glEditing = .F.
    

    Then use an IIF expression in the Caption setting:

    frsSet1.frmForm1.cmdButton1.Caption = ;   
       IIF(glEditing = .F., "Edit", "Save")
    

You could determine the size of a button and set the caption using expressions with fields in a table:

* set button width to length of 'Call ' + first and last names
frmForm1.cmdButton1.Width = 5 + ;
   LEN(ALLTRIM(employee.first_name    + " " + employee.last_name)) 
* set button caption to 'Call ' + first and last names
frmForm1.cmdButton1.Caption = "Call " + ;
   ALLTRIM(employee.first_name + " " + employee.last_name)

You could also set the caption using a user-defined function:

frsSet1.frmForm1.cmdButton1.Caption = setcaption()

Setting Multiple Properties

You can set multiple properties at once.

To set multiple properties

  • Use the WITH ... ENDWITH Command structure.

    For example, to set multiple properties of a column in a grid in a form, you could include the following statement in any event or method code in the form:

    WITH THISFORM.grdGrid1.grcColumn1
       .Width = 5
       .Resizable = .F.
       .ForeColor = RGB(0,0,0)
       .BackColor = RGB(255,255,255)
       .SelectOnEntry = .T.
    ENDWITH
    

Calling Methods at Run Time

After an object is created, you can call the methods of that object from anywhere in your application.

To call a method for an object

  • Use the following syntax:

    Parent.Object.Method

For example, the following code calls methods to display a form and set the focus to a command button:

* form set saved in MYF_SET.SCX
myf_set.frmForm1.Show
myf_set.frmForm1.cmdButton1.SetFocus

To hide the form, issue this command:

myf_set.frmForm1.Hide

See Also

Tasks

How to: Run a Form

How to: Hide a Form

How to: Pass Parameters to a Form

How to: Save a Form as HTML

Other Resources

Working with Forms

Creating Forms