You can run a form directly from the interface or in program code.
Running a Form Interactively
There are several ways to run the form you've designed.
If you're working in the Form Designer, you can test the form by clicking the Run button on the Form Designer toolbar. To reopen the form in the Form Designer, close the form or choose the Modify Form button on the toolbar.
You can also run a form from a project or programmatically.
To run a form
You can also run the form by choosing Do from the Program menu, choosing Form in the List Files of Type box, selecting the form, and choosing Do.
|When running a form, you can quickly switch to Design mode by clicking the Modify Form button on the Standard toolbar.|
To run a form from a program, include the DO FORM Command in the code associated with an event, in method code, or in a program or procedure.
Naming the Form Object
By default, when you use the DO FORM command, the name of the form object is the same as the name of the .scx file. For example, the following line of code runs Customer.scx. Visual FoxPro automatically creates an object variable for the form named
DO FORM Customer
To name a form object
Use the NAME clause of the DO FORM command.
For example, the following commands run a form, creating two form object variable names:
DO FORM Customer NAME frmCust1 DO FORM Customer NAME frmCust2
Manipulating the Form Object
You can associate a form object with a public variable so that you can access the form object through the variable name.
To associate a form object with a public variable
Use the DO FORM command in the Command window.
For example, the following commands, issued in the Command window, open a form named
Customer and change its caption.
DO FORM Customer Customer.Caption = "Hello"
If you then issue the following command in the Command window,
O is displayed in the active output window, indicating that
Customer is an object:
If you issue the DO FORM command in a program, the form object is scoped to the program. If the program or procedure completes, the object is gone, but the form remains visible. For example, you could run the following program:
*formtest.prg DO FORM Customer
After you run the program, the form remains visible and all of the controls on the form are active, but
U indicating that
Customer is an undefined variable. The following command, issued in the Command window, would generate an error:
Customer.Caption = "Hello"
Scoping the Form to the Form Object Variable
You can link a form to a form object.
To link a form to a form object
Use the DO FORM command with the LINKED keyword.
If you include the LINKED keyword, when the variable associated with the form object goes out of scope, the form is released.
For example, the following command creates a form linked to the object variable
DO FORM Customer NAME frmCust2 LINKED
frmCust2 is released, the form is closed.
Closing an Active Form
To allow users to close the active form by clicking the close button or by choosing Close from the form's Control menu, set the Closable property of the form.
To allow a user to close the active form
For example, you can close and release the
frmCustomer form by issuing the following command in a program or in the Command Window (Visual FoxPro):
You can also allow a user to close and release a form by including the following command in the Click Event code for a control, such as a command button with a caption of "Quit":
You can also use the RELEASE command in the code associated with an object on the form, but any code you've included in the Release Method will not be executed.
When you release a form, you release from memory the object variable created for the form. There is a single variable for a form set, so you can't release forms in a form set without releasing the form set. If you want to release the form set, you can use |