Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

ControlSource

This property lets you link (bind) a variable, field, or another property to a control’s Value.

Usage

oObject.ControlSource = cSource
cSource = oObject.ControlSource

Back in FoxPro 2.x, every control had to have what we called an “underlying variable”—something to hold the control’s value. Now that controls have their own Value property, that’s no longer necessary. You only have to bind something to a control when you want to. That’s what ControlSource is about.

Most of the controls have a couple of choices for data type. (For example, a check box’s Value can be either numeric or logical.) With these controls, the type of the item named in ControlSource determines the type of Value. Bind a logical field to a check box, and that check box’s Value is logical. Bind a numeric field, and the check box’s Value is numeric.

For lists and combos, ControlSource can be either character or numeric. When the ControlSource is numeric, its interpretation is determined by the control's BoundTo property. With the default .F. for BoundTo, Value (and the ControlSource) contains the position (or ListIndex) of the chosen item. To have a numeric value from the list's data stored in Value and the ControlSource, you have to set BoundTo to .T.

Although the ControlSource is bound to the control’s Value, the item named in ControlSource doesn’t get updated until after the control’s InteractiveChange is finished. Don’t test the value of a ControlSource until you reach the Valid event—if you need to test sooner, test the control’s Value. In fact, it’s better OOP to refer only to the control’s Value in its methods and not mention the ControlSource by name. If you reference the actual object named in ControlSource, the code is no longer well encapsulated and the control is no longer as easily reusable.

Example

This.ControlSource = "Employee.Last_Name"

See Also

BoundTo, InteractiveChange, ListIndex, Valid, Value