Advanced Visual FoxPro Controls

Ted Roche
Computer Resource
278 Kearsarge Avenue
Contoocook, NH
CIS: 76400,2503

Overview

How you'll really use controls in Visual FoxPro. Starting with a brief tour of the controls and how they differ from controls seen in 2.x, we'll review the concepts and implications of PEMs - Properties, Events and Methods - and how to alter them using the Property Sheet and Builders. We'll have a brief discussion of the OOP model and its implication for control development. Finally, we use VCXs and our own custom controls to develop a custom Form Controls Toolbar.


What controls are there?

Text-based controls: Label, TextBox, EditBox, Spinner

Pick-list controls: ComboBox, ListBox

Buttons: CommandButtons, OptionButtonsGroups, CheckBoxes

Containers; Grids, PageFrames, CommandButtonGroups

Graphical elements: Images, Lines, Shapes, Separators (toolbars only)

OLE: both OLEBound (associated with data) and Container (OCX) controls

Invisible control: timer


How do we work with controls?

Controls, in their simplest form, are simply the new versions of SAYs and GETs. But controls are so much more! They offer much finer control of the individual objects properties, the ability to change these properties at run-time, and the ability to define the code which should run when an event happens. Events are also much more numerous, giving us the chance to create interfaces more responsive to the user.

The controls supplied with Visual FoxPro are the starting point. These controls can (and should!) be subclassed to create our own custom controls. Multiple controls may be combined to form complex controls, better reflecting the complexity and business rules of the particular application. Custom controls can be created, saved and reused.

Properties

Properties describe a characteristic of a control. Most are available both at design and run-time, one exception being the class properties, which are read-only when the control is created. These properties are the data of the control which is "encapsulated" with the control. Most controls share a number of common properties:

 

Top, Height, Left, Width

The location on the form of the object, if a visual control

Comment

Probably the single most important property; let's you figure out what you were doing when you return to it

BaseClass, Class, ClassLibrary

The pedigree of the control

Name

How the control is named by all code within the form which refers to it

Visible

Whether the control should appear

DragMode, DragIcon

Behavior during mouse drag operations

HelpContextID

Your hook from the control into your custom help file

FontName, FontSize, FontItalic, FontBold, FontOutline, FontStrikethrough, FontUnderline

 For all text-based controls

ColorScheme, ColorSource, BorderColor, ForeColor, DisabledForeColor, BackColor, DisabledBackColor

Colors.

 

Then, there are properties specific to an individual control or two: Interval: how often a Timer control fires,

SpinnerHighValue, SpinnerLowValue, and a slew of others!


Events

Events occur when the user has taken some action, or programmatically when a control changes status. It is not possible to define new events. Common events include:

 

Init

Code run once when the control is created

Destroy

Code run when the control is released

DragDrop, DragOver

How to behave when a dragged object is over and dropped

MouseMove

Mouse movement over a control

Click, RightClick, DblClick, MouseDown, MouseUp

Both mouse buttons!

Error

How errors are handled

GotFocus, LostFocus

Code when control is tabbed to or clicked on

When, Valid

Our old friends.

Methods

Methods tend to be more individual to the controls, as they describe the unique behavior of the control. The most common are:

Drag

What to do if control is dragged

Move

Moves controls within a container

SetFocus

Programmatically "send" focus to a control

Refresh

Each time the control needs to be redisplayed, the code runs.

 

Individual methods include the Reset method for the Timer control, and the DoVerb methods for the two OLE controls.


Manipulating controls: Property Sheet & Builders

Property sheets, like our demo program, give us access to the various programmable and fixed properties, events and methods. Builders give an alternative view into a limited number of properties, simplifying the construction of objects.

Property sheets are tools which let us climb under the hood and tweak all the lev fonand dials. Builders don't have that depth, but don't have the complexity, either.

Figure A: Typical Property Sheet


Creating your own custom controls

You will want to create your own set of custom controls so that you can manipulate the base classes and modify their properties and behaviors. It is a good idea to create a set of 'generic' but subclassed controls and use these, rather than the standard toolbar, as the basis for prototyping screens. Create a custom control by placing a control on a form, highlighting it, then selecting 'Save as class..." from the "File" menu.

Most of the literature on the subject of OOP states that it is a learning process, and a different way of looking at problems. Anticipate that your first solution may not always be the best one. If possible, prototype your first development in Visual FoxPro on a system you can throw away. In subsequent systems, beware of the problem of over-engineering a solution.

Don't be afraid to throw out your work.

Don't subclass it to death - three or four levels is about all you'll comprehend.

 

Bibliography

There are a number of good books out there; here are a few of my favorites:

Windows Programming for Mere Mortals, Woody Leonhard

Object Oriented Analysis & Design, Grady Booch

The Art of Human-Computer Interface Design, Brenda Laurel, ed.

 

About the Speaker

Ted Roche is principal with Computer Resource of Contoocook, NH. His latest book project, with co-author Tamar Granor, is "The Hacker's Guide to Visual FoxPro for Windows" from Addison-Wesley. Ted was a contributing author to the best-selling "Using FoxPro 2.5", and was a technical editor for four FoxPro 2.5 books. Ted has published numerous journal articles and is currently a Contributing Writer and columnist for FoxPro Advisor magazine. He is a Microsoft Certified Professional in Windows and was named a Most Valuable Professional by Microsoft in 1994 for his support on the FoxForum and FoxUser forums of CompuServe.

Ted is a frequent lecturer at professional and user group conferences in North America and Europe. He is co-editor of the Boston Computer Network News, a Foxpro newsletter freely distributed on the Internet. He may be reached at (603) 746-5670 or by email at 76400.2503@compuserve.com