Learn Visual FoxPro the TakeNote Way!

Mention this document and receive 10% off your Visual FoxPro training!

Check out www.takenote.com for classes schedules and course outlines!

Group and On-site Rates Available!

Call (919) 870-9000 to register today!

 

About TakeNote…

TakeNote Computer Consulting is a Raleigh-based computer training and consulting firm specializing in Visual FoxPro. Since its inception in 1992, TakeNote has been serving the developer community with training in application development environments such as Visual FoxPro, Access and Visual Basic. TakeNote provides on-site training for national clients, as well as public training classes in Raleigh, NC.

 

About Our Visual FoxPro Training Classes…

TakeNote offers an extensive, hands-on Visual FoxPro training curriculum designed to bring developers with previous FoxPro experience and newcomers to Visual FoxPro up to speed quickly building robust, object-oriented, database applications. Public classes are held in a state-of-the-art training center with one student per computer in Raleigh, NC. Also, since our courseware is designed in-house, we can deliver a customized class at your site which can be targeted to meet the specific Visual FoxPro training needs of your development team.

Here are the Visual FoxPro classes we currently offer:

TNVFP5101: Introduction to Application Development With Visual FoxPro 5.0 (2 days)

TNVFP5201: Application Development With Visual FoxPro 5.0 (3 days)

TNVFP5202: Advanced Application Development With Visual FoxPro 5.0 (3 days)

TNVFP5301: Developing Client/Server Applications With Visual FoxPro 5.0 (2 days)

Check out our web site at http://www.takenote.com for course schedules and outlines. To register for a class, e-mail us at training@takenote.com or call 919-870-9000.

 

About Our Courseware…

Our courseware contains nearly 1,000 pages of Visual FoxPro tips, tricks, development techniques and hands-on, student exercises. Are you interested in providing Visual FoxPro training to your clients? Why devote the time to developing Visual FoxPro training courseware when ours is available for you to use. Our extensive line of Visual FoxPro training courseware is available for training organizations to purchase for use in their Visual FoxPro training courses or as a self-study guide for individuals who can't make it to a training class. Check out our web site at http://www.takenote.com or call for more information.

 

About Our Lead Instructor…

Jim Duffy is the founder and President of TakeNote Computer Consulting. Jim is an experienced trainer, author and developer with over 14 years of programming and training experience. His programming expertise, combined with his passion for teaching, qualify Jim to teach the skills necessary to become a proficient and effective application developer. Recently, Jim was one of the featured speakers at the 1997 Microsoft Visual FoxPro Developer's Conference (Devcon) where he presented sessions on Data Integrity & Multi-user Application Development Techniques and Using ActiveX Controls in Visual FoxPro.

Jim has trained hundreds of developers and has consistently been rated "Excellent" by students from organizations such as E.D.S., First Premier Bank, 360° Communications, Glaxo, Martin Marietta, Jefferson Pilot Insurance, Duke University, North Carolina State University, Florida State University, DuPont, and SAS Institute. Jim is a founding member of the Triangle FoxPro Users Group, where he is a frequent presenter and speaker. You can find some of Jim’s work published in such national publications as FoxPro Advisor and FoxTalk. His most recent article on data encryption appeared in the January ‘97 issue of FoxTalk.

When Jim is not presenting at a conference, seminar or user group meeting, you'll often find him teaching a class, developing Visual FoxPro applications or performing his stand-up comedy act at the local comedy club. Jim can be reached at jduffy@takenote.com.

 

Visual FoxPro Courseware Sample…

 

The following is one of the thirteen (13) modules from the 2-day, TNVFP5101: Introduction to Application Development With Visual FoxPro course.

 

Working With The Form Designer

 

 

12. Working With The Form Designer

12.1 Module Objectives

12.2 Form Terminology

12.3 Creating Forms

12.4 Saving Forms

12.5 Running a Form

12.6 Modifying An Existing Form

12.7 Form Designer and Form Control Toolbars

12.8 Student Exercise: Create a Form

12.9 Modifying Forms

12.10 Student Exercise: Color Palette and Layout Toolbars

12.11 Additional Form Techniques - Tool Tips

12.12 Student Exercise: Tool Tips

12.13 Additional Form Techniques - Tab Order

12.14 Student Exercise: Tool Tips and Setting the Tab Order

12.15 Module Review 

Module Objectives

In this section you will be introduced to the Form Designer and the process of creating forms (screens) in Visual FoxPro. Topics covered in this session include:

Basic Form Terminology

Creating, Saving, Modifying and Running a Form

Properties Window

Form Designer Toolbar

Form Controls Toolbar

Form Layout Toolbar

Color Palette Toolbar

Code Window

Tool Tips

Setting the Tab Order

 

 

 

Form Terminology

.SCX Files

Visual FoxPro stores all forms in .SCX files. An .SCX file is actually a Visual FoxPro table that contains all the information about the characteristics and the behaviors of all the objects on the form.

Controls

Controls are the objects that are placed on a form. The primary thing to remember when working with forms is that a control is any object on the screen, not just those that do something like a command button. Lines, shapes, text boxes, edit boxes, they are all controls. For example, you can place a text box or command button controls on a form.

Containers

Containers are objects that contain other objects and allow access to the objects it contains. The page frame and grid controls, as well as the form itself, are examples of containers.

Properties

A Property is an attribute or characteristic of an object. Each object on a form, as well as the form itself (or a set of forms) has a set of properties. Properties can be set at design time using the Properties window or at runtime through code. Properties include such things as colors, captions, position, size, and font. For example, the OK text that appears on a command button control is set in the Caption property of the command button.

Events

Each object or control recognizes certain actions called events. Events can be triggered by the system or the user. Events are triggered by things such as pressing a key, clicking on an object, or even by just moving the mouse over an object. For example, clicking on the OK button fires the Click event for the command button.

Methods

Methods are procedures associated with an object. A method associated with an event is executed whenever that event is triggered. For example, the Click event fires the Click method. If you write method code for the Click event of a command button, it is executed whenever the command button is pressed. A method that most controls have is the SetFocus method. To transfer the focus from the current object to a command button named cmdOverHere, use the following code:

 

THISFORM.cmdOverHere.SetFocus()

 

Naming Hierarchy

Referring to forms, controls and properties requires some new syntax and additional functions and keywords. Basically, you refer to all of these objects with a naming hierarchy that begins with the largest object and then moves down to each smaller object. For example, to change the Caption property of a command button named cmdSave in a form named frmCustomer, you’d use the following:

 

frmCustomer.cmdSave.Caption="Save It"

 

You could also use:

 

THISFORM.cmdSave.Caption="Save It"

 

This would eliminate the need to know the name of the currently running form. We will discuss the THISFORM statement in an upcoming section.

THIS

Often while programming, you will need to be able to reference the current control without knowing its name. For example, for five fields on a form, you need a routine that changes the color of the entry to Red if the number they type is too high. In that routine, you don’t need to know the name of the control you’re currently working with, just reference it with THIS. The Valid method for each of the 5 text boxes might look like this:

Example:

IF THIS.Value > 25

THIS.ForeColor=RGB(255,0,0)

ENDIF

In the above code, the Value property of the current control is compared to 25. If the value of the current control is greater than 25, the Forecolor property of the current control is set to red. The current control is referred to with the THIS keyword.

 

THISFORM

THISFORM refers to the form that the current control is contained in.

 

THISFORMSET

THISFORMSET refers to the form set that the current control is contained in.

Data Environment

The Data Environment is defined as all the tables, views, and relationships that are to be opened when you run or modify a form or report. It is saved with the form or report and can be modified in the Data Environment Designer.

Creating Forms

Overview

The process of creating forms involves placing controls on a data-entry form. In most cases, controls are placed on the form in one of two ways:

1. Through the use of a wizard which will build an entire form for you based on your answers to specific questions.

2. The final method for placing controls on a form is manually. Here, controls are placed by selecting the tool you want to use from the Controls toolbar and clicking on the form to place the control.

Wizards

Visual FoxPro comes with two Wizards that help make creating forms very easy. The Form Wizard creates a data-entry form from a single table.

 

 

The One-to-Many Wizard creates a data-entry form from two related tables, displaying records from the child table in a grid on the form.

 

 

Manually with Form Designer

Controls are manually placed on a form by selecting a tool from the Controls toolbar and then clicking on the form where you want the control to be placed.

Saving Forms

Using the Standard Toolbar

To save a form from the Standard toolbar, click on the Save button (the one with the picture of a diskette on it).

 

 

Using the Menu

To save a form using the menu, select the File | Save menu option.

Running a Form

Using the Standard Toolbar

To run a form click on the Run button (the red exclamation point button).

 

 

Using the Shortcut Menu

To run a form using the shortcut menu, right-click on the form to bring up the shortcut menu. Select Run to run the form.

Calling a Form Programmatically

The DO FORM command is used to call forms within your application

 

Syntax

 

DO FORM FormName | ?

[NAME VarName [LINKED]]

[WITH cParameterList]

[TO VarName]

[NOREAD] [NOSHOW]

 

The FormName argument specifies the name of the form or form set to run.

 

The NAME argument specifies an object variable name that the form will be referenced by. If the NAME argument is omitted, Visual FoxPro will creates an object variable with the same name as the form or form set file.

The WITH argument specifies the parameters that are passed to the form or form set. To pass parameters to a form, place the LPARAMETERS statement in the form’s Init method.

 The TO argument specifies that variable to hold a value returned from the form. To return a value from a form, place the RETURN command in the Unload event procedure of the form.

 

Modifying An Existing Form

Open a Form from the Project Manager

To open an existing form from the Project Manager, select the Document tab and click on the plus (+) sign next to the Forms icon to display all the forms in the project. Select the form you wish to open and click on the Modify button. The form will open up in the Form Designer.

 

 

Open a Form from the Command Window

To open an existing form from the Command Window, use the MODIFY FORM statement followed by the name of the form.

 

Example

 

MODIFY FORM TOTO

Open a Form Using the Menu

To open an existing form using the menu, select the File | Open menu option, select Files of Type: Form, select the location of the form, then select the form, and click the OK button.

 

 

 

Form Designer and Form Control Toolbars

Form Designer Toolbar

 

 

Set Tab Order: The Set Tab Order button displays the form in a mode for setting the tab order of objects.

Data Environment: The Data Environment button display the Data Environment window.

 

  

A data environment defines the source for the data used in a form or report. This can include tables, views, and relationships. It is saved with the form or report and can be modified whenever you are using the Report or Form Designer.

 

Properties Window: The Properties Window button displays the Properties window. The Properties window contains the list of properties, events, and methods for the selected object, be it a form, data environment, or control.

 

 

These are properties or attributes you set or change interactively at design time, or programmatically through code. You can also choose it from the View menu. The most common technique to display the Properties window is clicking the right mouse button in the Form Designer or Data Environment Designer and choosing Properties from the shortcut menu.

 

 

Tip: The shortcut menu for the Properties window offers customization of the Property window. You can specify whether or not property descriptions are displayed, change the display font, and only include properties that you've changed.

 

 

Code Window: The Code Window button displays the Code window.

 

The Code window is used to write, display, and edit method code. The Code window appears when you double-click a form or form control in Form Designer or double-click an event or method in the Properties window. Doing this has the same effect as choosing the Code command on the View menu.

 

Form Controls Toolbar: The Forms Controls Toolbar button shows or hides the Forms Controls toolbar.

 

Color Palette Toolbar: The Color Palette Toolbar button shows or hides the Color Palette toolbar.

Layout Toolbar: The Layout Toolbar button shows or hides the Layout toolbar.

 

Form Builder: The Form Builder button runs the Form Builder, which provides an easy, interactive way to add fields to the form as controls and/or define the form's style and layout.

Form Controls Toolbar

 

Label: A label is used to display text on the form, it does NOT have a data source. Even though a label can’t be directly edited by the user, events like Click can be associated with it.

Text Box: The text box is the basic control that lets the user enter information into a form.

Edit Box: The edit box is used to allow users to edit text from long character fields or memo fields. They provide automatic word wrapping and the ability to navigate through the text.

Command Button: The command button is an object that indicates that the user wants a specific action to occur. They are often used to save information, run reports, or leave a particular form by means of an OK or Cancel button. The command button can display a text label or a .BMP picture.

Command Group: The command button group is a container object that contains a group of command buttons that can be manipulated individually or as a group. You can assign method code to the click event of the group which will fire when any of the command buttons in the group are pressed. You can also assign method code to an individual button in the group. This method overrides the group method for that button.

Option Group: The option button group is a container object that contains option buttons that provide a short list of mutually exclusive options. Like the command button group, the option buttons in an option button group can be manipulated as a group or individually.

Check Box: A checkbox typically allows the user to specify Yes or No, True or False, On or Off, Open or Closed, etc. There is a third state that a check box can have, unknown. A check box will return 1 or true when checked, false or 0 when not checked, and 2 when it is neither true or false and will appear grayed out.

Combo Box: The combo box allows you to present a list of choices in a small area on the form by popping up a list when it is selected. The Style property determines the look and behavior of the combo box. If the Style is set to "Dropdown List" the user is only able to select from the list of items presented. If the Style is set to "Dropdown Combo" the user can either select from the list or type an entry directly into the combo box.

List Box: A list box control displays a list of items. You can select one or more items, display multiple columns, or display bitmaps as information in the list.

Spinner: A spinner is a control that allows the user to choose a range of numeric values. They allow the user to select a value either by spinning through the values or typing the value directly into the control.

Grid: The grid is a container control that provides the ability to view data in a spreadsheet-like control. The grid control displays data in rows and columns

Image: The Image control allows you to add .BMP files to a form. Like all other controls, it has the full range of properties, methods and events. As a result, images can be interacted with at runtime by clicking, double-clicking, dragging and dropping, and so on.

Timer: The Timer control is different because you never see it on the form at runtime. It provides a way to include periodic actions associated with a form. Enabled and Interval are the key properties of the timer control. The Enabled property is used to turn the timer on and off at runtime and the Interval property specifies the number of milliseconds between timer events. When the time interval is exceeded, the method code associated with the timer is executed.

Page Frame: The page frame control is a container control that contains multiple pages. The pages can include tabs that let you select a particular page by clicking on its tab. Controls can be placed on any number of individual pages or they can be placed so that they appear on all pages.

OLE Container Control: The OLE container control allows you to place an object from a different application on a Visual FoxPro form. For example, its possible to place an Excel spreadsheet on a form and use Excel to manipulate the spreadsheet.

OLE Bound Control: The OLE bound control allows you to edit an OLE object that has been stored in a general field in a table.

Line: The line control creates a vertical, horizontal or diagonal line. Although the line control is not associated with any data, like the label control, it can still respond to events such as click.

Shape: The shape control is a control for creating circles, ellipses or squares. Although the shape control is not associated with any data, like the label and line controls, it can still respond to events such as click.

Container: The container control is an object that is used to contain other objects and allow access to the objects contained within them.

Separator: The separator control is an object that is used to separate objects in a toolbar.

Button Lock: The Button Lock locks the current control for continued use.

Student Exercise: Create a Form

The first thing we’re going to do is to build a simple form introducing the process of creating a form manually and running it.

1. Create a blank form by selecting the Documents tab in the Project Manager, select Forms and click on the New button. Select New Form from the Create Dialog. The Form Designer will appear.

2. Save the form as Toto.

3. Make sure the Form Controls and Form Designer toolbars are open. If they are not, select View | Toolbars from the menu to open them.

4. Dock the toolbars by clicking the title bar and dragging them to the top of the screen.

5. Open the Properties window if it is not already open. Do this by right clicking on the form and selecting Properties from the shortcut menu or by selecting the Properties window icon from the Form Designer toolbar.

6. Review the Properties window and its associated shortcut menu including the Property Descriptions, Font, and Non-default property settings.

7. Open the Code window. Do this by right clicking on the form and selecting code from the shortcut menu or by clicking on the Code window icon in the Form Designer toolbar.

8. Review the Code window and the associated Object combo box and Procedure combo box. Close down the Code window.

9. Now, we’ll change one of the form’s properties, the Caption property.

 

Property

Value

Caption

Visual FoxPro is Cool!

 

10. Let’s add a label to the form. Click the Label tool in the Form Controls toolbar, move the mouse over the form and click and drag to size and place the label.

11. Make the following property changes for the newly added label.

 

Property

Value

AutoSize

.T.

Caption

Toto, we’re not in Kansas any more!

Name

lblNotInKansas

 

 12. Our first form wouldn’t be complete without a Hello World command button, so let’s add one to our form. Place a command button in the form by selecting the Command Button in the Form Controls toolbar, moving the mouse over the form, then clicking where you want the command button to be, and dragging to size the button in the form.

13. Make the following property changes for the newly added command button.

 

Property

Value

Caption

Hello World!

Name

cmdHello

 

14. Create a method that will display a "Hello World" message when the command button is clicked. Double click on the command button and the Code window appears.

15. Type the following in the Click procedure of the object called cmdHello: =MESSAGEBOX("Hello World!",48, "Does it Work??").

16. Place a second command button that will close the form when it is clicked. Make the following property changes for the newly added command button.

 

Property

Value

Caption

Close

Name

cmdClose

 

17. Place the following code in the Click event for cmdClose:

RELEASE THISFORM

18. Save the form.

19. Run the form. This can be done in several ways. You can click the Run icon in the Form Designer toolbar, or you can right click on the form and select Run from the shortcut menu.

20. When the form appears, click on the Hello World command button, click OK on the message box, and then try the Close button.

Modifying Forms

Moving Controls

Simply click on a control to select it and click-n-drag the mouse to move the control(s). For more precision movement, use the arrow keys on the keyboard.

Sizing Controls

Click on a control to select it and click-n-drag on the sizing handles that appear when a control is selected. To size controls with the keyboard, hold down the Shift key and use the arrow keys.

Deleting Controls

Before you can delete a control, it must be selected. Once the control is selected, press the Delete key on the keyboard to delete the control from the form.

Selecting Multiple Controls

Clicking on a control will select the control. There are several techniques to select multiple controls. One technique is to select a control, hold down the Shift key, and click on additional controls to be selected. Another technique, the lasso technique, involves clicking and dragging the mouse on the form, creating a selection box. Any control in the box or touching the box becomes selected.

Copying Controls

Before you can copy a control(s), it must be selected. Then you can select the Edit | Copy menu option (Ctrl+C) to copy the control to the clipboard. Selecting the Edit | Paste menu option (Ctrl+V) will place a copy of the control on the form.

Another technique you can use is to right-click the mouse, activating the shortcut menu. Select Copy to copy the control(s) to the clipboard.

Aligning Controls With the Layout Toolbar

Align Left Sides: Allows you to easily align the left sides of the selected controls.

Align Right Sides: Allows you to easily align the right sides of the selected controls.

Align Top Edges: Allows you to easily align the tops of the selected controls.

Align Bottom Edges: Allows you to easily align the bottoms of the selected controls.

Align Vertical Centers: Allows you to easily vertically center the selected controls.

Align Horizontal Centers: Allows you to easily horizontally center the selected controls.

Same Width: Allows you to make all the selected controls the same width.

Same Height: Allows you to make all the selected controls the same height.

Same Size: Allows you to make all the selected controls the same size.

Center Horizontally: Will automatically horizontally center all selected controls.

Center Vertically: Will automatically vertically center all selected controls.

Bring To Front: Brings the selected controls to the front of the stack.

Send To Back: Sends the selected controls to the back of the stack.

 

Adjusting Form and Control Colors with the Color Palette

Foreground Color: The foreground button in the color palette allows you to specify foreground colors such as what color text will appear in a text box.

Background Color: The background button in the color palette allows you to specify a control’s background colors such as what color an entire text box will be.

Student Exercise: Color Palette and Layout Toolbars

In this exercise we’ll continue with the Toto form created in the previous exercise. We’ll adjust the colors and layout of the form.

1. If it is not already open, open the Toto form from the previous exercise.

2. Open the Color Palette toolbar.

3. Click on an open area of the form and click on the Background Color button on the Color Palette toolbar. Select light gray by clicking on the light gray box. Try a few other colors, finish up with light gray.

4. Does the label on the form still have a white background? Change that by selecting the label and changing the BackStyle property.

 

Property

Value

BackStyle

2-Transparent

 

5. Add another label to the form and set it properties as follows:

 

Property

Value

BackStyle

2-Transparent

Caption

Ignore the man behind the curtain!

Name

lblIgnore

 

6. Save the form and test it.

7. Reopen the Toto form and make a copy of the cmdClose command button.

8. Select both Close buttons and experiment with the Layout toolbar.

9. When you’re done, delete the Close command button that is NOT named cmdClose.

10. Select the lblIgnore label. Click on the Foreground Color button in the Color Palette, then click on one of the green color boxes. Experiment with the different color combinations.

Additional Form Techniques - Tool Tips

Tool Tips

Move the mouse over any button in any tool bar and leave it there for a few seconds. A small text box will appear describing what the specific tool does. This is called Tool Tips and we can set Tool Tips for controls on our form.

Tool Tips are set through the Property Sheet. On the Layout tab in the Property Sheet, there is a property called ToolTipText. Enter the text as you want it to appear in the Tool Tip text box. Before Tool Tips will work you must set the ShowTips property for the form to True. False is the default.

Student Exercise: Tool Tips

In this exercise, you will assign Tool Tips to the controls on the form.

1. If it is not already open, open the Toto form from the previous exercise.

2. Open the Property sheet, and select Form1 from the Object drop-down list.

 

 

3. Select the Layout tab and change the ShowTips property to True by double clicking on the current setting of False (many properties can be set that way).

 

Property

Value

ShowTips

True

 

4. From the Object drop-down list select cmdClose.

5. Select the Layout tab and locate the ToolTipText property. Set the ToolTipText property.

 

Property

Value

ToolTipText

Close the Form

 

6. From the Object drop-down list select cmdHello.

7. Change the ToolTipText property for the cmdHello button.

 

Property

Value

ToolTipText

Hello World

 

 

8. Add a text box to the form using the Text Box tool from the Controls toolbar. Set its properties as follows:

 

Property

Value

Left

5

Name

txtName

Top

36

 

9. Add another text box. Set its properties as follows:

 

Property

Value

Left

5

Name

txtCity

Top

12

 

10. Save the form and run it. Press the tab key and watch the order in which controls are accessed. Close the form by pressing the Close button.

Additional Form Techniques - Tab Order

Setting the Tab Order for Controls

Tab Ordering is set by selecting the Tab Ordering button in the Form Designer toolbar Or by selecting the View | Tab Order menu option. 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.

There are two different methods for setting tab order. The first is Interactive, This is done by clicking the controls in the order you want them to be selected.

 

 

The second, By List, involves ordering a list of controls in a dialog box using the mover buttons next to each control. Interactive is the default technique.

 

If you don’t like the current setting for the Tab Order, you can switch to the other technique by selecting the Tools | Options menu option and clicking on the Forms tab. Select the technique you want to use from the Tab Ordering drop-down list. Click on the Set as Default button if you want the change to be remembered for your next Visual FoxPro session.

 

 

Show Position

Selecting the View | Show Position menu option will display the screen coordinates for the selected control in the status bar on the bottom of the screen.

Prompt To Save Changes

This option is used to tell Visual FoxPro if you want to be asked to save the form each time you run it. The default is on, you'll be more productive with it off.

Maximum Design Area

Use this option to limit the size of any forms you'll be creating. Even though you may have a 17" monitor and run in 1024x768 mode, you may not want to create any forms larger than a 640x480 VGA screen.

Student Exercise: Tool Tips and Setting the Tab Order

In this exercise, you will set the tab order for the controls on the form.

1. If it is not already open, open the Toto form from the previous exercise.

2. First, we’ll change the tab order interactively. Click on the Set Tab Order button in the Form Designer toolbar.

3. Next, click on the small blue box that is on the txtCity control. It changes to 1. Click on the controls in the order you want them selected. When you’re done, click on the form.

4. Save and run the form to test the new tab order.

5. Reopen the Toto form and we’ll change the tab order By List.

6. Select the Tools | Options menu option and select the Forms tab. Change the Tab Ordering drop-down list to By List. Click on OK to return to the Form Designer.

7. Click on the Set Tab Order button in the Form Designer toolbar. This time the Tab Order Dialog Box appears. Using the mover handles next to each control, make cmdHello the first control in the list followed by cmdClose. Click OK to return to the Form Designer.

8. Save and run the form to test the new tab order.

Module Review

In this section you were introduced to basic form techniques using the Form Designer. In this module you learned about:

Form Terminology

Creating, Saving, Modifying and Running a Form

The Properties Window

The Form Designer Toolbar

The Form Controls Toolbar

The Form Layout Toolbar

The Color Palette Toolbar

The Code Window

Tool Tips

Setting the Tab Order