FlexReport for WinForms | ComponentOne
In This Topic
    Modifying the Fields
    In This Topic

    You are not restricted to using VBScript to evaluate expressions in calculated fields. You can also specify scripts that are triggered when the report is rendered, and you can use those to change the formatting of the report.These scripts are contained in event properties. An event property is similar to a Visual Basic event handler, except that the scripts are executed in the scope of the report rather than in the scope of the application that is displaying the report.

    For example, you can use an event property to set a field's Font and ForeColor properties depending on its value. This behavior would then be a part of the report itself, and would be preserved regardless of the application used to render it.

    Of course, traditional events are also available, and you should use them to implement behavior that affects the application rather than the report. For example, you could write a handler for the StartPage event to update a page count in your application, regardless of which particular report is being rendered.

    The following table lists the event properties that are available and typical uses for them:

    Object Property Description
    C1FlexReport OnOpen Fired when the report starts rendering. Can be used to modify the ConnectionString or RecordSource properties, or to initialize VBScript variables.
    OnClose Fired when the report finishes rendering. Can be used to perform clean-up tasks.
    OnNoData Fired when a report starts rendering but the source recordset is empty. You can set the Cancel property to True to prevent the report from being generated. You could also show a dialog box to alert the user as to the reason why no report is being displayed.
    OnPage Fired when a new page starts. Can be used to set the Visible property of sections of fields depending on a set of conditions. The control maintains a Page variable that is incremented automatically when a new page starts.
    OnError Fired when an error occurs.
    Section OnFormat Fired before the fields in a section are evaluated. At this point, the fields in the source recordset reflect the values that will be rendered, but the report fields do not.
    OnPrint Fired before the fields in a section are printed. At this point, the fields have already been evaluated and you can do conditional formatting.

    In the following sections, you can explore the typical uses for these properties in detail.

    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:

    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

    Hiding a section if there is no data

    You can change a report field's format based on its data by specifying an expression for the Detail section's OnFormat property.

    For example, your Detail section has fields with an image control and when there is no data for that record's image you want to hide the record. To hide the Detail section when there is no data, in this case a record's image, add the following script to the Detail section's OnFormat property:

    If isnull(PictureFieldName) Then
      Detail.Visible = false
     Else
      Detail.Visible = true
    End If
    

    To hide a section if there is no data, in this case a record's image, for it, use an event script that looks like this:

    C1FlexReport1.Sections.Detail.OnFormat = "Detail.Visible = notisnull(PictureFieldName)"
    
    c1FlexReport1.Sections.Detail.OnFormat = "Detail.Visible = notisnull(PictureFieldName)";
    

    Alternatively, instead of writing the code, you can use the C1FlexReportDesigner to type the following script code directly into the VBScript Editor of the Detail section's OnFormat 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.OnFormat property, then click the drop-down arrow, and select Expression Editor from the list. VBScript Editor window appears.
    3. In the VBScript Editor:
      • Simply type the following script in the window:
        If isnull(PictureFieldName) Then
        Detail.Visible = false
        Else
        Detail.Visible = true
        End If

      • Or you could use the more concise version:
        Detail.Visible = not isnull(PictureFieldName)

    Showing or hiding a field depending on a value

    Instead of changing the field format to highlight its contents, you could set another field's Visible property to True or False to create special effects. For example, if you inserted a new field Shape field named "Shapefld" around the product name and set its Shape property to True, then you could write the script as follows:

       
    If UnitsInStock < ReorderLevel Then 
    Shapefld.Visible = True
    Else
    Shapefld.Visible = False
    End If
    

    To highlight products that are below the reorder level by displaying a box, use an event script that looks like this:

    Dim script As String = _   
      "If UnitsInStock < ReorderLevel Then" & vbCrLf & _
      "  BoxCtl.Visible = True" & vbCrLf & _   
      "Else" & vbCrLf & _   
      "  BoxCtl.Visible = False" & vbCrLf & _   
      "End If"   
    C1FlexReport1.Sections.Detail.OnPrint = script
    
    string script =     
      "if (UnitsInStock < ReorderLevel) then\r\n" +    
      "BoxCtl.Visible = true\r\n" +    
      "else\r\n" +     
      "BoxCtl.Visible = false\r\n" +     
      "end if\r\n";   
    c1FlexReport1.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 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 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 ellipsis next to the OnPrint property, to open VBScript Editor.
    3. In the VBScript Editor, simply type the following script:
      
      If UnitsInStock < ReorderLevel Then 
      Shapefld.Visible = True
      Else
      Shapefld.Visible = False   
      End If
      

    The following screen capture shows a section of the report with the special effects:

     Show or Hide a Shape Field in FlexReport

    Resetting page counter

    The C1FlexReport.Page variable is created and automatically updated by the control. It is useful for adding page numbers to page headers or footers. In some cases, you may want to reset the page counter when a group starts. For example, in a report that groups records by country. You can do this by adding code or using the Designer.

    Resetting page counter using Code:

    To reset the page counter when a group (for example, a new country) starts, set the PageFooter field's Text property. Enter the following code:

    C1FlexReport1.Fields("PageFooter").Text = "[ShipCountry] & "" "" & [Page]"
    
    c1FlexReport1.Fields["PageFooter"].Text = "[ShipCountry] + [Page]";
    

    Resetting page counter using FlexReportDesigner:

    To reset the page counter when a group (for example, a new country) starts, set the PageFooter field's Text property by completing the following steps:

    1. Select the PageFooter's page number field from the Properties window drop-down list in the Designer or select the field from the design pane. This reveals the field's available properties.
    2. Click the box next to the Text property, then click the drop-down arrow, and select Expression Editor from the list. VBScript Editor windows appears.
    3. In the VBScript Editor, type the following script:
      ="Page " & GroupPage(0) & " of " & GroupPages(0) & " for " & Country
    4. Click OK to close the editor.