Displaying expressions with the help of the "Text" object

Top  Previous  Next

Displaying expressions with the help of the "Text" object

Top Previous Next

      One of the most important features of this universal object is it’s ability to display not only a static text, but expressions as well. Expressions can be located in the object together with text. Let us examine a simple example of how it can be performed. Put the following line into the object:

 

Hello, World! Today is [DATE].

 

      Thus, when running the report, we can get something like follows:

 

Hello, World! Today is 01.01.2004.

 

      What lead to such result? During FastReport report building, if an expression enclosed in square brackets is encountered, the engine calculates it’s value and inserts the value into the text (in place of the expression). The “Text” object can contain any number of expressions, together with a usual text. Both single variables and expressions can be enclosed in brackets (for example, [1+2*(3+4)]). Any constants, variables, functions, and DB fields can be used in expressions. We will learn more about these features later, in the chapter.

 

      FastReport automatically recognizes expressions enclosed in square brackets in the text. But what should be done if our object contains square brackets, and we do not want them to be considered as expressions? For example, if we need to display such text as following:

 

a[1] := 10

 

      FastReport considers [1] as an expression, and displays the following:

 

a1 := 10

 

that is not what we want, of course. One of the ways to avoid such a situation is to disable the expression. Just disable the “AllowExpressions” property (“Allow Expressions” in the context menu), therefore all the expressions in the text will be ignored. In our example, FastReport would then display exactly what we need:

 

a[1] := 10

 

      Sometimes text is required to contain both an expression and a text in square brackets, for example:

 

a[1] := [myVar]

 

      Disabling of an expression allows us to display square brackets in the required place, but it also disables handling of expression. In this case, FastReport allows you to use another set of symbols to designate the expression. The “ExpressionDelimiters” property, which is equal to “[,]” by default, is responsible for it. In this case, the user can use angular brackets for the expressions, instead of square ones:

 

a[1] := <myVar>

 

      The “<,>” value must be set in the “ExpressionDelimiters” property. As you can see, the comma divides opening and closing symbols. There is one limitation however: the opening and closing symbols cannot be similar, so “%,%” will not work. One can set several symbols, for example “<%,%>” Thus, our example will look as follows:

 

a[1] := <%myVar%>