Working with FlexReport > Modifying the Fields > Formatting a Field According to Its Value |
Formatting a field according to its value is probably the most common use for the Section.OnPrint property. Take for example a report that lists order values grouped by product. Instead of using an extra field to display the quantity in stock, the report highlights products that are below the reorder level by displaying their name in bold red characters.
To highlight products that are below the reorder level by displaying their name in bold red characters, use an event script that looks like this:
Dim script As String = _ "If UnitsInStock < ReorderLevel Then" & vbCrLf & _ "ProductNameCtl.ForeColor = RGB(255,0,0)" & vbCrLf & _ "ProductNameCtl.Font.Bold = True" & vbCrLf & _ "Else" & vbCrLf & _ "ProductNameCtl.ForeColor = RGB(0,0,0)" & vbCrLf & _ "ProductNameCtl.Font.Bold = False" & vbCrLf & _ "End If" C1Flexreport.Sections.Detail.OnPrint = script
string script = "if (UnitsInStock& ReorderLevel) then\r\n" + "ProductNameCtl.ForeColor = rgb(255,0,0)\r\n" + "ProductNameCtl.Font.Bold = true\r\n" + "else\r\n" + "ProductNameCtl.ForeColor = rgb(0,0,0)\r\n" + "ProductNameCtl.Font.Bold = false\r\n" + "end if\r\n"; c1FlexReport1.Sections.Detail.OnPrint = script;
Alternatively, instead of writing the code, you can use the C1FlexReportDesigner application to type the following script code directly into the VBScript Editor of the Detail section's Section.OnPrint property. Complete the following steps:
If UnitsInStock < ReorderLevel Then
ProductNameCtl.ForeColor = RGB(255,0,0)
ProductNameCtl.Font.Bold = True
Else
ProductNameCtl.ForeColor = RGB(0,0,0)
ProductNameCtl.Font.Bold = False
End If
The control executes the VBScript code whenever the section is about to be printed. The script gets the value of the "ReorderLevel" database field and sets the "ProductName" report field's Field.Font.Bold and Field.ForeColor properties according to the value. If the product is below reorder level, its name becomes bold and red.
The following screen capture shows a section of the report with the special effects: