Displaying expressions with the help of the "Text" object

Top  Previous  Next

One of the most important features of this basic object is its ability to display not only a static text but expressions as well. Expressions can be located within the object mixed in with normal text. Let's see a simple example of how this works. Type the following into the object:


Hello, World! Today is [DATE].


When the report is run we can get something like this:


Hello, World! Today is 01.01.2004.


How does this happen? When FastReport creates the report and encounters an expression enclosed in square brackets the report engine calculates the expression's value and inserts this value into the text in place of the expression. “Text” objects can contain any number of expressions mixed in with the normal text. Complex expressions can contain brackets (for example [1+2*(3+4)]). Constants, variables, functions and DB fields can all be used in expressions. We will learn more about these later in the chapter.


FastReport automatically recognizes expressions enclosed in square brackets in the text. But what happens if our normal text contains square brackets which we do not want to be considered as expressions? For example, if we need to display the following:


a[1] := 10


FastReport would consider [1] as an expression and display the text as:


a1 := 10


which is not what we want, of course. One way to avoid this happening is to disable expression recognition. Disable the “AllowExpressions” property (or “Allow Expressions” in the context menu) and all expressions in the text will be ignored. In our example, FastReport would then display exactly what we need:


a[1] := 10


But sometimes text is required to contain both an expression and normal text with square brackets, for example:


a[1] := [myVar]


Disabling “AllowExpressions” lets us display square brackets in the required places, but it also disables handling of expression. In this situation FastReport allows you to use an alternative set of symbols to designate expressions. The “ExpressionDelimiters” property, “[,]” by default, is responsible for this. In our example we can use angular brackets for the expressions instead of square ones:


a[1] := <myVar>


The “<,>” value must be set in the “ExpressionDelimiters” property, where the comma is required to separate the opening and closing symbols. Another requirement is that the opening and closing symbols cannot be identical, so “%,%” will not work. Complex symbols can be used, for example “<%,%>”. So our example could look like this:


a[1] := <%myVar%>