You can use forms to give your users a familiar interface for viewing and entering data into a database, but forms provide far more than just an interface. Forms provide a rich set of objects that can respond to user (or system) events so that you can enable your users to accomplish their information management tasks as easily and as intuitively as possible.
Designing Forms
Visual FoxPro provides you with a powerful Form Designer to make form design fast and easy. You can have:
•
Various types of objects on the forms.•
Data bound to objects on the form.•
Top-level or child forms.•
Multiple forms that can be manipulated together.•
Forms based on your own custom templates.Forms and form sets are objects with their own properties, events, and methods that you can set in the Form Designer. A form set consists of one or more forms that can be manipulated as a unit. If you have four forms in your form set, for example, you can display or hide them as a unit with a single command at run time.
Creating a New Form
You can create new forms in the Form Designer, and you can see how each object will appear to the user as you design it.
To create a new form
•
In the Project Manager, select Forms and choose New.–
Or
–
•
From the File menu, choose New, select Form, and choose New File.–
Or
–
•
Use the CREATE FORM command.Setting the Data Environment
Each form or set of forms includes a data environment. The data environment is an object that includes the tables or views the form interacts with and the relationships between tables that the form expects. You can visually design the data environment in the Data Environment Designer and save it with the form.
The data environment can automate opening and closing tables and views when the form is run. In addition, the data environment helps you set the ControlSource property for controls by populating the ControlSource property setting box in the Properties window with all the fields in your data environment.
To open the Data Environment Designer
1
From the View menu, choose Data Environment.
2
From the Shortcut menu, choose Add.
3
In the Open dialog box, choose a table or view to add to the data environment.
Common Data Environment Properties
The following data environment properties are commonly set in the Properties window:
Property
Description
Default Setting
AutoCloseTables
Controls whether tables and views are closed when the form is released.
True (.T.)
AutoOpenTables
Controls whether tables and views in
the data environment are opened when the
form is run.
True (.T.)
InitialSelectedAlias
Specifies the table or view that is selected when the form is run.
"" at
design time. If not specified, at run time the first cursor added to the
DataEnvironment is initially selected.
Adding a Table or View to the Data Environment Designer
When you add tables or views to the Data Environment Designer, you can see the fields and indexes that belong to the table or view.
To add a table or view to the Data Environment
1
From the Data Environment Designer, choose Add from the DataEnvironment menu.
2
In the Add Table or View dialog box, choose a table or view from the list.
–
Or
–
If no database or project is open, choose Other to select a table.
You can also drag a table or view from an open project or the Database Designer into the Data Environment Designer.
When the Data Environment Designer is active, the Properties window displays objects and properties associated with the data environment. Each table or view in the data environment, each relationship between tables, and the data environment itself is a separate object in the Object box of the Properties window.
Removing a Table from the Data Environment Designer
When you remove a table from the data environment, any relationships that the table is involved in are also removed.
To remove a table or view from the Data Environment Designer
1
In the Data Environment Designer, select the table or view.
2
From the DataEnvironment menu, choose Remove.
Setting Relationships in the Data Environment Designer
If you add tables to the Data Environment Designer that have persistent relationships set in a database, the relationships are automatically added in the data environment. If the tables don’t have persistent
relationships, you can still relate them in the Data Environment Designer.
To set relationships in the Data Environment Designer
•
Drag a field from the primary table onto the matching index tag in the related table.Creating Single- and Multiple-Document Interfaces
Visual FoxPro allows to you to create two types of applications:
•
Multiple-document interface(MDI) applications consist of a single main window, and theapplication’s windows are contained within or float on top of the main window. Visual FoxPro is primarily an MDI application, with the command window, edit windows, and designer windows contained within the main Visual FoxPro window.
•
Single-document interface(SDI) applications consist of one or more independent windows, each ofwhich appears separately on the Windows desktop. Microsoft Exchange is an example of an SDI application, in which each message you open appears in its own independent window.
An application consisting of single window is usually an SDI application, but some applications mix SDI and MDI elements. For example, Visual FoxPro displays its debugger as an SDI application, which in turn contains MDI windows of its own.
To support both types of interfaces, Visual FoxPro allows you to create several types of forms:
•
Child form. A form contained within another window, used in creating MDI applications. Child formscannot be moved outside the bounds of their parent form (the main form), and when minimized appear at the bottom of their parent form. If their parent form is minimized, they are minimized along with it.
•
Floating form. A form that belongs to a parent (main) form, but is not contained within it. Instead,floating forms can be moved anywhere on the screen. They cannot be moved behind their parent window. If minimized, a floating form appears at the bottom of the desktop. If their parent form is minimized, floating forms are minimized along with it. Floating forms are also used in creating MDI applications.
•
Top-level form. An independent form without parent form, used to create an SDI application, or toserve as the parent for other child forms in an MDI application. Top-level forms work at the same level as other Windows applications, and can appear in front of or behind them. They appear on the Windows taskbar.
Specifying a Form Type
You create all types of forms in much the same way, but you set specific properties to indicate how the form should behave.
If you are creating a child form, you specify not only that it should appear inside another form, but also whether it is an MDI-compliant child form, which indicates how the form behaves when maximized. If the child form is MDI-compliant, it combines with the parent form, sharing the parent form’s title bar and caption, menus, and toolbars. A child form that is not MDI-compliant maximizes into the full client area of the parent, but retains its own caption and title bar.
To specify a child form
1
Create or edit the form using the Form Designer.
2
Set the form’s ShowWindow property to one of the following values:
•
0 – In Screen. The child form’s parent will be the main Visual FoxPro window.•
1 – In Top-Level Form. The child form’s parent will be the top-level form that is active when thechild window is displayed. Use this setting if you want the child window to appear inside any top-level window other than the main Visual FoxPro window.
3
Set the form’s MDIForm property to .T. (true) if you want the child form to be combined with the
parent when maximized, or to .F. (false) if the child window should be retained as a separate window when
maximized.
A floating form is a variation of a child form.
To specify a floating form
1
Create or edit the form using the Form Designer.
2
Set the form’s ShowWindow property to one of the following values:
•
0 – In Screen. The floating form’s parent will be the main Visual FoxPro window.•
1 – In Top-Level Form. The floating form’s parent will be the top-level form that is active when thefloating window is displayed.
3
Set the form’s Desktop property to .T. (true).
To specify a top-level form1
Create or edit the form using the Form Designer.
2
Set the form’s ShowWindow property to 2 – As Top-Level Form.
Displaying a Child Form Inside a Top-Level Form
If you have created a child form in which the ShowWindow property is set to 1 – In Top-Level Form, you do not directly specify the top-level form that acts as the child form’s parent. Instead, Visual FoxPro assigns the child form to a parent at the time the child window is displayed.
To display a child form inside a top-level form
1
Create a top-level form.
2
In the event code of the top-level form, include the DO FORM command, specifying the name of
the child form to display.
For example, create a button in the top-level form, then in the Click event code for the button, include a command such as this one:
Note The top-level form must be visible and active when the child form is displayed. Therefore, you cannot
use the Init event of the top-level form to display a child form, because the top-level form will not yet be active.
3
Activate the top-level form, and then if necessary, trigger the event that displays the child form.
Hiding the Main Visual FoxPro Window
If you are running a top-level form, you might not want the main Visual FoxPro window to be visible. You can use the Visible property of the Application object to hide and show the main Visual FoxPro window as needed.
To hide the main Visual FoxPro window
1
In the Init event of the form, include the following line of code:
Application.Visible = .F.
2
In the Destroy event of the form, include the following line of code:
Application.Visible = .T.
Make sure that you also provide a way to close the form by using
THISFORM.Release in some method
or event.
Note You can also include the following line in a configuration file to hide the main Visual FoxPro window:
SCREEN = OFF
Adding a Menu to a Top-Level Form
To add a menu to a top-level formCreate a top-level form menu.
Set the form’s ShowWindow property to 2 – As Top-Level Form.
3
In the Init event of the form, run the menu program and pass it two parameters:
DO menuname.mpr WITH oForm, lAutoRename
oForm is an object reference to the form. In the Init event of the form, pass THIS as the first parameter. lAutoRename specifies whether or not a new unique name is generated for the menu. If you plan to run
multiple instances of the form, pass .T. for lAutoRename.
For example, you can call a menu called
mySDImenu with this code:
DO mySDImenu.mpr WITH THIS, .T.
Extending Forms with Form Sets
You can manipulate multiple forms as a group by including them in a form set. A form set has these benefits:
•
You can show or hide all the forms in a form set at one time.•
You can visually arrange multiple forms at once to control their relative positions.•
Because all the forms in a form set are defined in a single .SCX file with a single data environment, you can automatically synchronize record pointers in multiple forms. If you change the record pointer in a parent table in one form, the child records in another form are updated and displayed.Note All the forms and all the objects on the forms are loaded when you run the form set. Loading many
forms with a lot of controls might take several seconds.
Adding Objects to Forms
To design the functionality you want in a form, you add the appropriate controls, set form and control properties, and write event code.
You can add the following types of objects to a form:
•
Controls•
Containers•
User-defined classes•
OLE objectsUnderstanding Container and Control Objects
Objects in Visual FoxPro belong in one of two categories, depending on the nature of the class they are based on:
•
Containers can hold other containers or controls. They can act as the parent object for otherobjects. For example, a form, as a container, is the parent object of a check box on that form.
•
Controls can be placed in containers, but cannot be the parent for other objects. For example, acheck box cannot contain any other object.
The Form Designer allows you to design both containers and controls.
Container
Can contain
Column
Headers, and any objects except form sets, forms, toolbars, timers, and other
columns
Command button group
Command buttons
Form set
Forms, toolbars
Form
Page frames, grids, any controls
Grid
Columns
Option button group
Option buttons
Page frame
Pages
Page
Grids, any controls
Adding Visual FoxPro Containers
In addition to form sets and forms, Visual FoxPro provides four base container classes. Command button group
Option button group
Grid
Page frame
To add container objects to a form
•
In the Form Controls toolbar, select the desired container object (button group, grid, or page frame) button and drag it to size in the form.When you add a command button group or an option button group to a form in the Form Designer, the group contains two buttons by default. When you add a page frame to a form, the page frame contains two pages by default. You can add more buttons or pages by setting the ButtonCount property or the PageCount property to the number you want.
When you add a grid to a form, the ColumnCount property is set to – 1 by default, which indicates AutoFill. At run time, the grid will display as many columns as there are fields in the RowSource table. If you don’t want AutoFill, you can specify the number of columns by setting the grid’s ColumnCount property.
Collection and Count Properties
All container objects in Visual FoxPro have a count property and a collection property associated with them. The collection property is an array referencing each contained object. The count property is a numeric property indicating the number of contained objects.
The collection and count properties for each container are named according to the type of object that can be contained in the container. The following table lists the containers and the corresponding collection and count properties.
Application
Objects
Forms
Count
FormCount
FormSet
Forms
FormCount
Form
Objects
Controls
Count
ControlCount
PageFrame
Pages
PageCount
Page
Controls
ControlCount
Grid
Columns
ColumnCount
CommandGroup
Buttons
ButtonCount
OptionGroup
Buttons
ButtonCount
Column
Controls
ControlCount
ToolBar
Controls
ControlCount
Container
Controls
ControlCount
Control
Controls
ControlCount
These properties allow you to use a loop to programmatically manipulate all or specific contained objects. For example, the following lines of code set the BackColor property of columns in a grid to alternating green and red:
o = THISFORM.grd1
FOR i = 1 to o.ColumnCount
IF i % 2 = 0 && Even-numbered column
o.Columns(i).BackColor = RGB(0,255,0) && Green
ELSE
o.Columns(i).BackColor = RGB(255,0,0) && Red
ENDIF
ENDFOR
Adding Visual FoxPro Controls to a Form
You can easily add any of the standard Visual FoxPro controls to your form by using the Controls toolbar.
Standard Visual FoxPro Controls
Check box
Image
OLE Bound Control
Text box
Combo box
Label
OLE Container Control
Timer
Command button
Line
Shape
Edit box
List box Spinner
To add controls to a form
•
In the Form Controls toolbar, select the desired control button and click or drag it to size in the form.Adding Data-Bound Controls to a Form
You can bind controls to data in a table, view, table field, or view field by setting the ControlSource property of a control to a field or the RecordSource property of a grid to a table or view. But you can also create data- bound controls by dragging fields or tables to the form directly from:
•
The Project Manager•
The Database Designer•
The Data Environment DesignerThe class of control created this way depends on the Field Mappings settings in the Properties tab of the Table Designer or the Field Mapping tab of the Options dialog box.
For more information about setting default control classes, see the Table Designer or the Field Mapping tab of the Options dialog box.
Manipulating Objects
There are several ways you can manipulate objects at design time:
•
Set the size and position of objects by dragging them in the Form Designer window.•
Align controls by choosing alignment tools on the Layout toolbar or options on the Format menu.•
Set colors by choosing foreground and background colors in the Palette toolbar.•
Set properties in the Properties window. The primary point of control for all objects in your form is the Properties window.Setting Properties at Design Time
The Properties window opens with the properties or events of the selected object displayed. If more than one object is selected, the properties that the objects have in common are displayed in the Properties window. To edit the properties or events of a different object, choose the appropriate object from the Object box or select a different control in the form.
To set a property
1
In the Properties window, select a property in the Property and Events list.
2
In the Property Settings box, type or choose the desired setting for the selected property.
Note Properties that are read-only at design time, such as the Class property of an object, are displayed in
the Properties and Events list in the Properties window in italics.
If the property requires a character value, you don’t have to include the value in quotation marks. If you want the caption of a form to be CUSTOMER, type CUSTOMER in the Property Settings box. If you want the caption of a form to be “CUSTOMER”, with the quotation marks displayed in the window title, type
“CUSTOMER” in the Property Settings box.
Setting Properties with Expressions
You can also set properties to the results of expressions or functions through the Properties window.
To set a property with an expression
•
In the Properties window, choose the Function button to open the Expression Builder.–
Or
–
•
In the Property Settings box, type = followed by an expression.For example, you can set the Caption property of a form to indicate the currently active table when the form is run by typing =ALIAS(
) in the Property Settings box.
A property expression is evaluated when the you set it in the Properties window and when the object is