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 using code:
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"
c1r.Sections.Detail.OnPrint = script
• C#
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";
c1r.Sections.Detail.OnPrint = script;
The code builds a string containing the VBScript event handler, and then assigns it to the section's Section.OnPrint property.
To highlight products that are below the reorder level using the C1ReportDesigner:
Alternatively, instead of writing the code, you can use the C1ReportDesigner to type the following script code directly into the VBScript Editor of the Detail section’s Section.OnPrint property. Complete the following steps:
1. Select Detail from the Properties window drop-down list in the Designer. This reveals the section's available properties.
2. Click the empty box next to the Section.OnPrint property, then click the drop-down arrow, and select Script Editor from the list.
3. In the VBScript Editor, simply type the following script in the window:
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
4. Click OK to close the editor.
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:
|