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.

Controls, ControlCount

These properties let you access all the controls in a container without knowing what they are. ControlCount tells you how many there are, while Controls is an array where each element contains a reference to one control. Controls is also referred to as a collection. (Other collections in Visual FoxPro include Forms and Pages.)

Usage

nControls = oObject.ControlCount
oControl = oObject.Controls( nControlNumber )
oObject.Controls( nControlNumber ).Property = uPropertyValue
uPropertyValue = oObject.Controls( nControlNumber ).Property
oObject.Controls( nControlNumber ).Method()

ControlCount can’t be changed. It’s just a way for you to find out how many controls there are.

Controls, on the other hand, is a handle—it lets you grab the controls in a container and look at them, manipulate them, or do whatever you like to them. Be careful, though—different controls have different properties and methods, and you can’t just loop through, doing the same thing to every control. Also, remember that Controls only gets you to the top level. If the control you’re looking at is a container itself (like a command group or page frame), you still may have to dig down into that container to get at everything on the form. Fortunately, each container type either has a unique collection of its own (like Buttons or Pages) or has its own Controls collection (Column or Page, for example).

It’s often easier to manipulate properties with the SetAll method than to loop through Controls. In fact, SetAll is more thorough. It digs down through multiple layers of containment. With the Controls collection, you get just the ones at the top level and you have to drill down manually. The example below shows one piece of the manual operation.

We expect to use SetAll whenever we want to make global changes and save the Controls collection for cases where we don’t want to drill down or when we need to examine properties of the objects, not just change them, or when we need to call methods.

Beginning in VFP 5, most containers also have an Objects collection, despite documentation to the contrary. As of VFP 7, every container has an Objects collection. This is an ActiveX collection (that is, it uses Windows ActiveX capabilities) rather than a native VFP collection. It provides a handle to each of the contained objects of whichever object you’re looking at. Be aware, however, that not every Objects collection in VFP has a Count Property to tell you how many there are. Many do, but notably, the Objects collection of a grid does not have a Count Property (but the Objects collection of a column within the grid does). Go figure.

Example

* In VFP 5 and later, FOR EACH can be used to loop
* through the collection. This avoids the need for the
* nCounter variable and means you don't need to check
* ControlCount.
LOCAL nCounter
FOR nCounter = 1 TO ThisForm.ControlCount
   * Make sure the property you're interested in exists before
   * changing it.
   IF TYPE("ThisForm.Controls[nCounter].BackColor") = "N"
      ThisForm.Controls[nCounter].BackColor = RGB(255,0,0)
   ENDIF

   * If it's an option button group, dig down and do the
   * contained buttons, too.
   IF UPPER(ThisForm.Controls[nCounter].BaseClass) = ;
      "OPTIONGROUP"
      ThisForm.Controls[nCounter].SetAll("BackColor", ;
          RGB(255,0,0))
   ENDIF
ENDFOR

* SetAll does everything above and more.
ThisForm.SetAll("BackColor", RGB(255,0,0))

See Also

ButtonCount, Buttons, ColumnCount, Columns, FormCount, Forms, Objects, PageCount, Pages, SetAll