Formatting a field according to its value is probably the most common use for the 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:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
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 |
To write code in C#
C# |
Copy Code
|
---|---|
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 OnPrint property.
To highlight products that are below the reorder level using the C1FlexReportDesigner:
Alternatively, instead of writing the code, you can use the C1ReportDesigner application to type the following script code directly into the VBScript Editor of the Detail section's 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
To highlight products that are below the reorder level using the C1ReportDesigner:
To highlight the 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:
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: