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.

DynamicBackColor, DynamicForeColor

These properties are among the things that make grids so cool. Using these properties, you can specify conditions for each column of a grid to base its colors on anything you want (though you’ll usually base them on data in the grid). So, you can show negative balances in red, or color a grid like a checkerboard, or show sales on Sunday in purple on white. In fact, you can even drive your users to distraction by basing a column’s colors on things like the day of the week or the time of day.

Usage

colColumn.DynamicBackColor = cColorExpression
cColorExpression = colColumn.DynamicBackColor
colColumn.DynamicForeColor = cColorExpression
cColorExpression = colColumn.DynamicForeColor

These properties have a level of indirection that BackColor and ForeColor don’t. You don’t specify a particular color—you give a string containing an expression that, when evaluated, returns a color. Our experience is that, most often, the expression involves IIF(). However, any valid character string that evaluates to a number in the appropriate range will do. You can, for example, add a method to the form that figures out what color you want and call that routine. The most important thing is to remember to enclose the expression in quotes so it gets evaluated at runtime—not when you specify it.

The specified expressions are re-evaluated for each visible row each time the column is refreshed. Be careful how much computation you include, since too much can bog the system down.

Example

* Suppose you have a form containing a grid showing the
* TasTrade Products table. Some products are discontinued.
* Rather than having a separate column to indicate that,
* you could show those products in reverse video.
* For each column, the DynamicBackColor is set as
* (assume This is the grid):
This.DynamicBackColor = "IIF(Products.Discontinued, ;
                             This.ForeColor, This.BackColor)"
* And set the DynamicForeColor as:
This.DynamicForeColor = "IIF(Products.Discontinued, ;
                       This.BackColor, This.ForeColor)"

See Also

BackColor, DynamicAlignment, DynamicFontBold, DynamicFontItalic, DynamicFontName, DynamicFontOutline, DynamicFontShadow, DynamicFontSize, DynamicFontStrikeThru, DynamicFontUnderline, DynamicInputMask, ForeColor, RGB()