ComponentOne FlexReport for WinForms
Formatting a Field According to Its Value
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 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"
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;

To highlight products that are below the reorder level using FlexReportDesigner:

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:

  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 Expression Editor from the list. VBScript Editor window appears.
  3. In the VBScript Editor window, type the following script:
           

    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:

Formatting FlexReport Fields