How to dynamically change the color of a control based on a condition
Updated: Sep 3, 2020
As you know the appearance of a control on page depends on two properties: Style and StyleExpr. The first one is an option list of possibile styles (eg. Favorable, Unfavorable, Attention etc), and the second one accepts (seems to...) a boolean expression.
Normally you choose a Style and when the boolean expression in the StyleExpr property is true, the appearance of the control changes to the option you set in the Style property.
However in this way you can just manage one style.
Suppose now to have a FactBox like the one below, in which the field Final Inventory must have the Favorable style when positive and the Unfavorable one when negative.
How can you set the Style dynamically? The solution is quite easy but not immediately intuitive, if you follow IntelliSense suggestions.
To solve this issue, follow these steps:
Declare a Text variable
Set the variable with the Style option you want into the OnAfterGetRecord trigger
Associate the variable to the StyleExpr of the control
Below you can see the solution.
Please note that the association between the StyleExpr property and the SetStyle procedure is not accepted.