Demo Script: Using the ProgressBar Control in Visual FoxPro

Robert Green, Microsoft Corporation
June 5, 1997

Purpose of this Document

This document is designed as part of a series of demo script documents to assist customers who are evaluating Microsoft® Visual FoxPro™ 5.0. It can be used by an individual developer learning a new feature in Visual FoxPro 5.0; or as the basis for a demo shown to other developers and software tools evaluators. This demo script requires Visual FoxPro 5.0 to be installed. Questions about this demo script should be directed via email to foxmktg@microsoft.com.

Introduction

ActiveX™ controls (formerly known as OLE controls) are reusable software components that can be added to existing applications with minimal additional coding. Using ActiveX controls, developers can add unique functionality to their applications using prebuilt and pretested components. With over 2,000 ActiveX controls currently available, Visual FoxPro developers can access a rich inventory of components to extend and enhance their applications. Developers can use the tools in Microsoft Visual Studio 97™ or Visual Basic® Control Creation Edition to create their own redistributable ActiveX controls.

One of the key enhancement areas for Visual FoxPro 5.0 is improved support for ActiveX controls. With Visual FoxPro 5.0, developers can choose from a wider variety of ActiveX controls. Visual FoxPro 5.0 ships with a set of ActiveX controls(.OCX files) you can add to and distribute with your applications. ActiveX controls are added to a form in an application by using the Visual FoxPro OLE container control.

ActiveX controls are installed by default if you choose the Complete installation option when you install Visual FoxPro. If you did not choose this option, you can run Visual FoxPro Setup again at any time and install only the ActiveX controls. ActiveX controls are installed in the SYSTEM directory in Windows 95® or the SYSTEM32 directory in Windows NT®.

The purpose of this document is to illustrate the power and simplicity of working with ActiveX controls in Visual FoxPro. The ProgressBar control is used because it shows the progress of a lengthy operation by filling a bar with rectangles from left to right.

Note that the ProgressBar control is part of a group of ActiveX controls that are found in the COMCTL32.OCX file. Other controls in this file include the ImageList, ListView, Slider, StatusBar, TabStrip, Toolbar, and TreeView controls. Help topics for these controls are in Ctrlref.hlp, located in the Visual FoxPro Ctrlhelp folder.

Add Controls to a Form

In this example you will create a form that monitors the progress of a batch validation routine. The validation involves looking at every record in a table of orders and making sure that the net amount of the order is equal to the discounted gross amount of the order plus the freight.

Create a new form by typing create form in the Command window. Set the properties of the form as follows:

Property

Value

AutoCenter

.T.

Caption

Progress Bar ActiveX Control Demo

Height

84

Width

468

Click on the OLE Container Control button on the Form Controls toolbar. Position the mouse cursor anywhere on the form and then drag until you have a rectangle. In the Insert Object dialog box choose Insert Control. The Control Type list then shows all of the ActiveX controls registered on your machine. Choose Microsoft ProgressBar Control from the list and then choose OK. Set the properties of the control as follows:

Property

Value

Appearance

1 - 3D

BorderStyle

1 – Fixed Single

Height

24

Left

24

Name

ocxProgressBar

Top

18

Width

324

Add a label to the form to inform the user what is happening. Set the properties of the label as follows (the caption will be set at run time):

Property

Value

Height

18

Left

24

Top

54

Width

253

Add a command button that will be used to start the validation process. Set the properties of the command button as follows:

Property

Value

Caption

Validate

Default

.T.

Height

37

Left

384

Name

cmdValidate

Top

12

Width

60

Add Code to the Form and Controls

You are now ready to add code to the form to validate the orders table and update the progress bar.

Add the following code to the Init method of the form. This code will be run automatically when the form is started. The code initializes the progress bar and removes the caption of the form's label.

* Blank out the caption of the label.
ThisForm.label1.Caption = ""

Add the following code to the Click method of the Validate button. This code validates the orders table and updates the progress bar.

* Have the label tell what is happening.
ThisForm.label1.Caption = "Validating orders table..."
ThisForm.Refresh
Select orders
* The Min and Max properties set the progress bar's range.
* The Min defaults to 0. Set the Max to the number of 
* records in the orders table.
ThisForm.ocxProgressBar.Max = RECCOUNT()
* Initialize a counter to represent the current record.
i = 1
* Loop through each record in the orders table.
SCAN
    * Set the net amount of the order.
    IF order_net <> (order_amt * order_dsc/100) + freight
        Replace order_net With ;
          (order_amt * order_dsc/100) + freight
    ENDIF
    * The Value is the current position in the range.
    * Set it to the current counter value. The control control knows 
    * when to draw another rectangle based on its size.
    ThisForm.ocxProgressBar.Value = i
    * Increase the counter by 1.
    i = i + 1
ENDSCAN

* Reset the progress bar.
ThisForm.ocxProgressBar.Value = 0
* Have the label report that the operation is finished.
ThisForm.Label1.Caption = "Orders table validated"
ThisForm.Refresh

Save and run the form. Choose Validate to start the validation process. The caption of the label displays what is happening and the progress bar updates steadily until the process is over.

Behind the Scenes

The progress bar has a range and a current position. The range represents the entire duration of the operation. The current position represents the progress the application has made toward completing the operation. ActiveX controls are programmed via their properties and methods, just like Visual FoxPro objects. The Max and Min properties of the ProgressBar control set the limits of the range. The Value property specifies the current postion within the range.

The Min property of the control defaults to zero. The Max property is set to the number of records in the orders table. Therefore, the Max property is the number of records that need to be processed. In addition, a counter variable is created. This will keep track of what record is current.

ThisForm.ocxProgressBar.Max = RECCOUNT()
i = 1

As the code loops through each record, the net amount of the order is checked and recalculated if necessary. The Value property of the progress bar is set to the counter variable. In this example, that means the Value is increased by 1 after each record is validated.

ThisForm.ocxProgressBar.Value = i
i = i + 1

When the Value of the progress bar is changed, the control may or may not draw another rectangle. Remember that the Value property specifies the current position within the range. Because rectangles are used to fill in the control, the amount filled in only approximates the Value property's current value.

Based on the control's size, the Value property determines when to display the next rectangle. The Height and Width properties of the control determine the number and size of the rectangles that fill the control. The more rectangles, the more accurately the control portrays an operation's progress. To increase the number of rectangles displayed, decrease the control's height or increase its width.

Summary

ActiveX controls are a powerful way of reusing components to speed application development and simplify maintenance. ActiveX controls enable Visual FoxPro developers to quickly add unique functionality to their applications, and to share that functionality among several development and runtime environments. Visual FoxPro 5.0 has enhanced support for ActiveX controls, making available a wider range of controls for the Visual FoxPro developer. The programming model for ActiveX controls is simple and straightforward, and similar in many respects to Visual FoxPro native objects.

Frequently-Asked Questions

How can I learn more about using ActiveX controls with Visual FoxPro 5.0?

A good place to start is the white paper entitled, "ActiveX Controls and Visual FoxPro 5.0," found on the Visual FoxPro Web site. Also, see Chapter 16, "Adding OLE," in the Developer's Guide, part of the documentation that came with your copy of Visual FoxPro 5.0.

Can Visual FoxPro 5.0 create ActiveX controls?

Visual FoxPro is an application development environment that focuses on assembling applications from components, rather than on building components per se. Microsoft provides several tools for component creation (Visual C++®, Visual Basic®, Visual Basic - Control Creation Edition, Visual J++™).

What other products can use ActiveX controls?

All of the tools in Visual Studio 97™, plus Microsoft® Office 97 and Microsoft® Internet Explorer. ActiveX controls are a standard component of Windows, so many other third-party products support them as well.

How are a control's properties and methods documented?

In many cases, controls install with help files (.HLP) which describe how to program them. In cases where no help files are included, developers can use an Object Browser which ships with several other Microsoft tools, to inspect the control's properties, events, and methods.

Can I obtain an evaluation copy of Visual FoxPro 5.0?

Visual FoxPro 5.0, and all Microsoft products, are available with a 30-day money back guarantee from software resellers. Therefore, if you purchase the product on a trial basis, and decide not to keep it, simply return it for a full refund.

How do I get the Visual Basic Control Creation Edition (VBCCE)?

Visual Basic - Control Creation Edition is free, available for download. Please see www.microsoft.com/vbasic/ for more information.


© 1997 Microsoft Corporation. All rights reserved.