次の方法で共有


Setting Properties at Run Time

The object model in Visual FoxPro gives you a great deal of control over properties at run time.

Referencing Objects in the Object Hierarchy

To manipulate an object, you need to identify it in relation to the container hierarchy. At the highest level of the container hierarchy (the form set or form) 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 .scx file.

Properties are manipulated by referencing the object variable, the control, and the property, separated by dots (.):

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

  • Assign an expression to the property.

    -or-

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

The syntax for calling methods of an object is:

Parent.Object.Method

Once an object has been created, you can call the methods of that object from anywhere in your application. The following commands call 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

Responding to Events

The code you include in an event procedure is executed when the event takes place. For example, the code you include in the Click event procedure of a command button runs when the user clicks the command button.

Calling the procedural code associated with an event does not cause the event to occur. For example, the following statement causes the code in the Activate event of frmPhoneLog to be executed, but it doesn't activate the form:

frmPhoneLog.Activate

Calling the Show method of a form causes the form to be displayed and activated, at which point the code in the Activate event is executed:

frmPhoneLog.Show

See Also

Running a Form | Example of Manipulating Objects | Creating Forms | Hiding a Form | Passing Parameters to a Form | Saving a Form as HTML