Chapter 8: Managing Data Through Forms

Just as reports make it easy to print your tables and queries, forms provide a convenient way to display, enter, and edit your database information. You can create forms that mimic the paper forms you’re used to, providing a familiar data entry environment.

This chapter shows how to create interactive forms from tables and views, using form wizards, builders, and the Form Designer. For information about using forms as the basis for building an application, see Chapter 9, Creating Forms, in the Programmer’s Guide.

This chapter discusses:

Creating Forms

Forms display the fields and records in your tables and views, and usually include navigation controls to help you move from record to record.

You can create forms in Visual FoxPro in any of these ways:

Using a Form Wizard

Whenever you want to create a new form, you can use a form wizard to help you set it up. The wizard will ask you a series of questions and build a form for you based on your answers. You can choose from several different style options and preview your form before creating it.

To create a form with a wizard

  1. In the Project Manager, select the Documents tab and select Forms.

  2. Choose New.

  3. Choose Form Wizard.

  4. Select the type of form you want to create.

  5. Follow the instructions on the wizard screens.

    Two form wizards are available to help you create forms.

You can also access the form wizards from the menu by choosing the Wizards command from the Tools menu, and selecting Form.

Visual FoxPro provides two different form wizards to help you create forms:

Forms you create with the form wizards have a standard set of navigation buttons so you can display different records in the form, edit records, search for records, and so on. If you create a form within a database, the Form Wizard can use input mask and format settings stored in the database.

Starting the Form Designer

If you want to create your own form without using a wizard, use the Form Designer. With the Form Designer, you can add fields and controls to your form, and customize it by adjusting and aligning the controls.

The Quick Form command is provided to make it easier to get started creating a form. The Quick Form command displays the Form Builder, which adds selected fields from tables or views to your form.

To create a new form

  1. In the Project Manager, choose the Documents tab.

  2. Select the Forms icon.

  3. Choose New.

  4. Select New Form.

The Form Designer window appears so you can begin creating your form.

The Form Designer window

Adding Fields Quickly

When you want to quickly place the fields from a table or view into a form, choose Quick Form on the Form menu. Quick Form starts the Form Builder, which adds selected fields from a table or view to your form, using the field style you choose.

The Form Builder helps you quickly create a form from a table or view.

To add fields to a form with the Form Builder

  1. In the Form Designer, select Quick Form from the Form menu.

  2. Make your field and style choices in the Form Builder and choose OK to generate the form.

The Form Builder creates a form without navigation controls, so you can add your own. You can use a control builder to add navigation controls to your form, or choose from the predefined library of navigation controls provided with Visual FoxPro. For more information, see Adding Controls to a Form later in this chapter.

Saving Forms

When your form is finished, you can save it for future use.

To save a form

Forms are saved as files with an .scx extension.

Running a Form

Once you have saved a form, you can run it to see how it works.

To run a form

You can also choose the Run button on the Standard toolbar.

Tip   When running a form, you can quickly switch to design mode by clicking the Modify Form button on the Standard toolbar.

Adding Controls to a Form

You add new controls by selecting them from the Form Controls toolbar, and placing them in the Form Designer window. For example, you might want to add new labels for fields on the form, add new controls such as buttons, edit boxes, list boxes, and so on, or add pictures or lines and shapes to enhance the appearance of your form.

To add a control to a form

  1. Choose a control in the Form Controls toolbar.

  2. In the Form Designer window, drag to create a rectangle where you want the control to be, or click to create a control of default size.

The new control appears where you placed it. You can then move it to its final location on the form, and resize it as needed.

If you want help adding a control, you can use a builder to add the controls and make them work with the form, or you can add the controls using the Form Controls toolbar and the Properties window.

Adding Controls to a Form with a Builder

Just as wizards can be used to construct forms quickly, builders can be used for adding controls to a form. The builder asks a series of questions related to the control you are adding to your form, and sets the appropriate properties to make the control work as desired.

For example, if you want to add a new text box to a form, the builder asks which style of text box you prefer and which table’s field it should display.

Builders can be accessed in one of two ways: from the Form Designer shortcut menu, or by activating the Builder Lock button on the Form Controls toolbar when you add controls to a form.

To add a control with a builder

  1. In the Form Controls toolbar, select the Builder Lock button.

  2. Select the control you want from the Form Controls toolbar, and draw it on the form.

  3. Fill in the information on the builder tabs.

Adding Fields to a Form with the Form Builder

A quick way to add a new field to a form is to use the Form Builder. Even if you already have fields on your form, you can add new fields that match the style of existing fields. You can also choose different sources for the fields you add to the form, making it easy to add fields from other tables or views to your form.

To add fields to a form

  1. In the Project Manager, open the form.

  2. From the Form menu, choose Quick Form. The Form Builder appears.

  3. In the Style tab, select the style you want for the new control.

  4. In the Field Selection tab, select the source of the fields, and the fields you want to add.

  5. Choose OK.

Using Builders for All Controls

By setting the Builder Lock button on the Form Controls toolbar, you can automatically use a control builder each time you add a control to a form.

To turn on the Builder Lock

Adding Controls to a Form with the Component Gallery

You can use the Component Gallery to quickly add custom controls and objects to a form. The Component Gallery contains dozens of custom controls and objects that greatly reduce the amount of coding required to create specialized forms.

To open the Component Gallery

When the Component Gallery is open, you can drag custom controls and objects from the Component Gallery and drop them on a form.

Adding Controls to a Wizard-Generated Form

If you create a form with one of the form wizards and then want to add controls to it, you can match the style of the existing controls by using the controls in the wizard controls library, Wizstyle.vcx.

When you open the wizards control library, wizard-style controls (such as chiselfield, embossedmemo, and so on) are available in the Form Controls toolbar. To find out which control to use, select an existing control and note its class and class library. For example, a standard wizard field would be in the Standardfield class, found in Wizstyle.vcx.

Tip   Use ToolTips on the Form Controls toolbar to help you identify class names.

To match controls in a wizard-generated form

  1. In the Form Designer, open the form.

  2. From the Form Controls toolbar, choose the View Classes button and choose Add.

  3. In the Open dialog box, locate Wizstyle.vcx in the Wizards subdirectory and choose OK.

  4. In the Form Controls toolbar, select the control that corresponds to an existing control in the form and drag in the form to add it.

    Note   If you move a form to a new directory or another system, you should move the control library associated with the form along with it.

Adding Controls and Setting Control Properties

To add a new control to a form without using a builder, just select the control you want from the Form Controls toolbar, and drop it on the form.

Some controls, such as labels, shapes, and lines, do not display data from a table or view, or perform an action. But most other controls do, and you need to tell Visual FoxPro what field you want them to display, or what action you want them to perform. For example, you usually want a text box to display the value from a field, and a button to execute a command.

To make new controls work properly in your form, you need to associate, or bind them with the table and field you want them to display. You link the control to its control source — the data you want it to display or the field where you want entered data to be stored — by setting the ControlSource property in the Properties window.

For example, if you add a new text box to a form, you need to tell Visual FoxPro where to get the values for the text box by setting the ControlSource property to point to a field in a table or view. You can then set other properties to control the appearance of the control.

A tool called the Data Environment Designer makes it easy to add a new field to a form and set the ControlSource property for the field.

Setting Up the Data Environment

By placing the tables or views associated with your form in the data environment for your form, you make it easy to bind new controls to the fields in the tables or views.

When you have finished setting up a data environment, the ControlSource property field displays the fields available in the Data Environment Designer so you can select the one you want to bind to the control.

If you are editing a form created by a wizard, you will find that the data environment for your form is already populated with the tables or views you used when setting up the form with a Form Wizard.

To display the data environment

To add a table or view to the data environment

  1. From the Data Environment menu, choose Add.

    Note   If the data environment is empty, the Add Table or View dialog box appears automatically.

  2. In the Add Table or View dialog box, select Tables or Views.

  3. Select the table or view you want to use.

  4. Choose Add.

    Note   To add views to the Data Environment Designer, you must have a database open.

Adding a Control to a Form

The Form Controls toolbar makes it easy to add a new control to your form so you can display a field from your table or view. You’ll want to select the type of control appropriate to the type of field you are displaying. For example, use a text box control to display a single-line field or select an edit box control to display a memo.

The Properties window displays all the properties for the controls you add to a form. You use the ControlSource property in the Properties window to bind the control you have added to your form to a specific field in the table in your Data Environment Designer.

To display the Properties window

To add a new field to a form

  1. In the Form Controls toolbar, choose a data control and drag in the Form Designer window to create the control.

  2. In the Properties window, choose the Data tab and select the ControlSource property.

  3. Enter a field name.

    -or-

    Select a field from the list of available fields.

    Available fields for the ControlSource property of a control

Adding Multiple Controls at One Time

When you choose the Button Lock button, you can add multiple controls of the same type without having to click the control button on the toolbar more than once. For example, if you want to add five text boxes to a form, make sure the Button Lock button is selected, and then click the text box control button in the Form Controls toolbar once and add all five text boxes to the form.

Button lock is automatically engaged if you double-click on a control button on the Form Controls toolbar. To turn button locking off, click the Button Lock button again or click the Selection Pointer button on the Form Controls toolbar.

Adding Controls Quickly

You can quickly create single or multiple controls by clicking on fields or tables and dragging them directly onto a form. You can drag fields or tables from any of the following data sources and drop them on a form or a container class that you are designing:

When you drag a field or table onto a form, Visual FoxPro determines what type of control to create based on options you set.

Setting Field Mapping Options

You can specify the type of control that is created when you drag a field or table onto a form. For example, you can create a text box control any time a Character field is dragged onto a form.

To map field types to classes

  1. From the Tools menu, choose Options.

  2. Select the Field Mapping tab.

  3. To change a field type mapping, select a line in the Map fields to classes for drag and drop area and choose Modify.

  4. In the Modify Field Mapping dialog box, select a field type from the Type list.

    To set the class that is created whenever you drag a table or multiple fields, select Multiple from the Type list.

  5. To select the class library (.vcx file) that contains the control you want to associate with the selected field type, choose Browse.

  6. Select a class name from the Name list. The class you specify here will be created any time a field of the selected type is dragged onto a form.

  7. Choose OK to accept the mapping.

  8. Set options in the Database options area:
    To… Select…
    Create a label in addition to the bound control when you drag a field or table to a form or container Drag and drop field caption
    Set the Comment property of the bound control to the text specified in the Field Comment box in the Fields tab of the Table Designer Copy field comment
    Set the InputMask property of the bound control to the input mask specified in the Fields tab of the Table Designer Copy field input mask
    Set the Format property of the bound control to the format specified in the Fields tab of the Table Designer Copy field format

Note   You can also specify classes for dragging and dropping in the Display Class box in the Fields tab of the Table Designer. The settings in the Table Designer override the settings you specify here.

Quickly Creating a Single Control

You can quickly create a single control based on the field type mapping you specified on the Field Mapping tab in the Options dialog box. You can also override the default mapping and create a different class type.

To create a single control

To create a single control and override the current field mapping

  1. Right-click on any field in the Data Environment and drag it onto a form.

  2. From the shortcut menu, select Create Other Control Here.

  3. In the Open dialog box, choose a Visual Class Library (.vcx) file.

  4. Select the type of control you want to create from the Class Name box and choose Open.

Quickly Creating Multiple Controls

You can drag an entire table onto a form. By default, a grid control is created when you drag a table onto a form. You can override the default and create multiple controls corresponding to the default field type mappings specified for each field in the table.

To create a grid control

To create multiple controls

  1. Right-click any table, or two or more highlighted fields in a table, in the Data Environment, Database Designer, or Project Manager, and drag it onto a form.

  2. From the shortcut menu, select Create Multiple Controls Here.

Modifying a Form

If the wizard- or builder-generated forms don’t quite fit your needs, you can modify them with the Form Designer. Using the Form Designer, it’s easy to move and resize controls, copy or delete controls, align controls, and modify the tab order.

To modify a generated form

  1. In the Project Manager, select the Documents tab.

  2. Choose the Forms icon and select the name of the form.

  3. Choose Modify.

Using the Form Designer Toolbars

The Form Designer toolbars give you quick access to frequently used commands and the layout, alignment, and color controls you need when designing or modifying forms. For more information about the purpose of each button on each toolbar, you can use the ToolTips feature or search for “Toolbars”.

The Form Designer toolbars

Selecting, Moving, and Resizing Controls

After creating a form, you may need to adjust the placement or size of the controls on the form. For example, you might want to move a memo field, or make a text box longer.

To select a control

To select adjacent controls

  1. Choose the select arrow in the Form Controls toolbar.

  2. By dragging the pointer, draw a box around the controls you want to select.

To move a control

  1. Select the control by clicking it.

  2. Drag the control to the new position in the form window.

    -or-

    Select the control and use the arrow keys to reposition it.

To resize a control

  1. Select the control.

  2. Drag one of the sizing handles to increase the length, width, or overall size of the control.

Copying and Deleting Form Controls

When designing or modifying a form, you may want to make a copy of a control which is already on the form. You can copy an existing control and paste the copy onto your form.

To copy a control

  1. Using the pointer, select a control.

  2. From the Edit menu, choose Copy.

  3. From the Edit menu, choose Paste.

  4. Use the pointer to reposition the control as needed.

If you create a control and then decide you don’t need it, or don’t want a control created by a wizard, you can remove the control.

To delete a control

Aligning Controls

The buttons on the Layout toolbar make it easy to accurately line up controls in relation to each other in your forms. For example, you might want to align controls horizontally or vertically or make sure that a related set of controls has the same width or height. To align controls, select a group of controls, and then choose one of the layout buttons on the Layout toolbar.

For more information, see “Aligning Controls” in Chapter 7, Designing Reports and Labels, and see the Layout Toolbar topic.

Adjusting Control Placement

If you need to place controls precisely on the screen, you can use Show Position on the View menu. When checked, Show Position displays the coordinates and dimensions of a selected control in the status bar at the bottom of the Form Designer window.

Control coordinates and dimensions shown in the status bar

Controlling the Grid Display

The grid display helps you align controls on your forms. You can adjust the size of the grid with Set Grid Scale on the Format menu. You can turn display of the grid on and off by using the Grid Lines command on the View menu, or setting the Grid Lines option on the Forms tab of the Options dialog box.

You can also adjust the size or position of controls in relation to the grid with Snap to Grid on the Format menu. When Snap to Grid is checked on the Form menu, controls placed on a Form are automatically aligned to the guidelines on the grid. You can override the grid by:

Setting Tab Order for Controls

The tab order of a form determines the sequence in which the controls are selected when a user presses the TAB key to move through the form.

You can choose two different methods for setting tab order: interactively, by clicking the controls in the order you want them to be selected when the form is used; or by arranging a list of controls in a dialog box.

To choose a tab ordering method

  1. From the Tools menu, choose Options.

  2. In the Options dialog box, choose the Forms tab.

  3. Under the Tab Ordering option, select Interactive or By List.

Setting Tab Order Interactively

In interactive mode, you set tab order by double-clicking the control you want to be first in the tab order, and then single-clicking on the rest of the controls in the order in which you want them to be selected in the form.

To change the tab order interactively

  1. From the View menu, choose Tab Order.

  2. Double-click the tab order box beside the control you want to be the first control in the tab order.

  3. Click the tab order box for each of the other controls.

  4. Click anywhere on the form to save your changes and exit Tab Order mode, or press ESC to exit Tab Order mode without saving your changes.

Setting Tab Order by List

In list mode, you set tab order by rearranging the control names in the Tab Order dialog box. You can set order by row (down the form) or by column (across the form).

Setting tab order by arranging a list of controls

To set the tab order using a list

  1. From the View menu, choose Tab Order.

  2. Choose By Row or By Column.

  3. In the Tab Order dialog box, use the pointer to rearrange the list.

  4. Choose OK.

Customizing Forms

You can add enhancements to your forms to make them more interesting visually and easier to use. For example, you might want to group similar controls together visually by drawing a box around them, or use bitmaps to give the forms a more professional appearance.

Using the Form Designer, you can customize your forms by:

Changing Text Fonts and Sizes

Using the font properties in the Properties window, you can modify the style and size of the text that appears in controls you have added to your forms. You can choose from the available font styles and sizes supported by your system. To change the text in your form, change the font properties, such as FontName, FontSize, and FontBold, shown in the Properties window.

To change the text in your forms

  1. In the Form Designer, select the control you want to change.

  2. In the Properties window, drop down the Object list, and select the object you want to change.

  3. Locate the font property you want to change, and set its value.

Adding Shapes and Lines to Forms

You can add shapes and lines to forms to customize them. For example, you may want to separate groups of controls with a dividing line, or enclose related controls within a box to set them apart.

To add a line to a form

  1. In the Form Controls toolbar, choose the Line button.

  2. Drag in the form to create a line.

You can size and move the line like other controls, or change the slant of the line with the LineSlant property in the Properties window.

To add a shape to a form

  1. In the Form Controls toolbar, choose the Shape button.

  2. Drag in the form to create a shape.

You can change the type of shape drawn with the Curvature property in the Properties window.

Adding Graphics to a Form

You can add a graphic to the background of your form, or add a picture to a box inside the form.

To add a graphic to your form

  1. Choose one of the following:
  2. In the Properties window, select the Picture property.

  3. Choose the dialog button to display the Open dialog box.

  4. Locate the bitmap or other file you want to use.

  5. Choose OK.

Setting Form Colors

You can control a form’s colors by using the ColorSource property to base the colors on an existing color scheme, or you can use the Color Palette toolbar to change the foreground or background color of your forms and the controls within them.

To set form colors using the ColorSource property

To set the foreground or background color of a form or control

  1. From the View menu, select Color Palette toolbar.

  2. Choose Foreground Color or Background Color.

  3. Select the control or form you want to change.

  4. In the Color Palette toolbar, select the color you want to use.

  5. To see more color choices, choose Other Colors on the toolbar.

To create a custom color

  1. In the Color Palette toolbar, choose Other Colors.

  2. In the Color dialog box, choose Define Custom Colors to display the Custom Color Selector.

  3. Select one of the Custom Colors boxes, and then click in the Custom Color Selector to select the custom color you want.

  4. Choose Add to Custom Colors to add the new color to your palette.

  5. Choose OK.

    Visual FoxPro applies the custom color to the current selection.

Setting the Maximum Form Design Area

Setting the form design area lets you customize forms to ensure they display properly under specific resolutions. For example, if you know that users with 640 x 480 screen resolution will be using the form you are designing, you should not select 1024 x 768 as your maximum design area.

To set the maximum form design area

  1. From the Tools menu, choose Options.

  2. In the Forms tab, select a value for Maximum Design Area.

    Tip   If you select None, the Form Designer will not impose a limit on the size of form you can design.