Depending on the type of report you are using, Section or Page/RDL, there are different ways to change control properties at run time. In this article, we will take a look at the procedure for each report type.

Code-Based Section Reports


In a Section report, you can change control properties at run time by adding code to report events. Example code in Visual Basic and C# looks like this:

'Visual Basic
Private Sub Detail_Format(…) Handles Detail.Format
  'Change the value of the Text property for a Label control.
  Dim s As String

  Select Case Me.Label1.Text
    Case "1" : s = "string 1"
    Case "2" : s = "string 2"
    Case "3" : s = "string 3"
    Case Else : s = "string X"
  End Select

  Me.Label1.Text = s

  ' (**1) Move a Label control from its original
  ' location to: (X,Y) = (1,0)
  Me.Label2.Location = new System.Drawing.PointF(1, 0)

  ' (**2) Set the Font property for a Label
  ' control to: "Bold,Arial,14pt,Italic"
  Me.Label3.Font = new System.Drawing.Font("Arial", 14.0F,
    FontStyle.Bold Or FontStyle.Italic,
    GraphicsUnit.Point,1)
End Sub


//C#
private void detail_Format(object sender, EventArgs e)
{
  //Change the value of the Text property for a Label control.

  string s;
  switch (this.label1.Text)
  {
    case "1": s = "string 1"; break;
    case "2": s = "string 2"; break;
    case "3": s = "string 3"; break;
    default: s = "string X"; break;
  }
  this.label1.Text = s;

  // (**1) Move a Label control from its original
  // location to: (X,Y) = (1,0)
  this.label2.Location = new System.Drawing.PointF(1,0);

  // (**2) Set the Font property for a Label
  // control to: "Bold,Arial,14pt,Italic"
  this.label3.Font = new System.Drawing.Font("Arial", 14.0F,
    FontStyle.Bold | FontStyle.Italic,
    GraphicsUnit.Point,1);
}


(**1) You cannot directly change members of the Location object (e.g., Location.X) that are returned from this property, because Location property values are indicated in numeric type. To change the Location property at run time, you need to create a new instance of the System.Drawing.PointF class to match the data type of the Location property.

(**2) The values of the Font property are ReadOnly, so, for example, you cannot directly change the value of Font.Name. To change the Font property at run time, you need to create a new instance of the Font object.

Important: You can change a control's properties only within the events for the section that contains the control (e.g. Detail_Format, or PageHeader_BeforePrint, or GroupFooter_AfterPrint), or within the ReportStart event. You can also change properties at the report instance creation level in code outside of the report with a private or public modifier.

For more information, see the following topics in the ActiveReports help file:

Section Report Events | Create Red Negatives Report | Create Green Bar Report

XML-Based Section Reports


In an XML-Based Section report (and section reports in the End User Designer), you can change control properties at run time by adding script to report events in the script tab. Example code in Visual Basic and C# looks like this:

'Visual Basic
Sub Detail_Format
  'Change the value of the Text property for a Label control.
  Dim s As String

  Select Case Me.Label1.Text
    Case "1" : s = "string 1"
    Case "2" : s = "string 2"
    Case "3" : s = "string 3"
    Case Else : s = "string X"
  End Select

  Me.Label1.Text = s

  ' (**1) Move a Label control from its original
  ' location to: (X,Y) = (1,0)
  Me.Label2.Location = new System.Drawing.PointF(1, 0)

  ' (**2) Set the Font property for a Label
  ' control to: "Bold,Arial,14pt,Italic"
  Me.Label3.Font = new System.Drawing.Font("Arial", 14.0F,
    FontStyle.Bold Or FontStyle.Italic,
    GraphicsUnit.Point,1)
End Sub


//C#

public void Detail_Format()
{
  //Change the value of the Text property for a Label control.

  string s;
  switch (this.Label1.Text)
  {
    case "1": s = "string 1"; break;
    case "2": s = "string 2"; break;
    case "3": s = "string 3"; break;
    default: s = "string X"; break;
  }
  this.Label1.Text = s;

  // (**1) Move a Label control from its original
  // location to: (X,Y) = (1,0)
  this.Label2.Location = new System.Drawing.PointF(1,0);

  // (**2) Set the Font property for a Label
  // control to: "Bold,Arial,14pt,Italic"
  this.Label3.Font = new System.Drawing.Font("Arial", 14.0F,
    FontStyle.Bold | FontStyle.Italic,
    GraphicsUnit.Point,1);
}


Page and RDL Reports



In a Page or RDL Report, you can change control properties at run time by adding code to expressions in the control properties. See examples below.

Note: You can set an expression in any property whose type is ExpressionInfo. However, you cannot change control properties like Location or Size. Since a Page report is designed to have output that looks exactly like the layout looks at design time, we don't allow size and location settings to be changed at run time.

Change the output value

To change the output value, in the Properties Window, find the control's Value property and click the drop-down arrow button to open the expression editor and enter an expression like the following (replacing FieldName with the name of your field):

=Switch(Fields!FieldName.Value="1","string1",
Fields!FieldName.Value="2","string2",
Fields!FieldName.Value="3","string3",
Fields!FieldName.Value<>"","stringX")


Conditional formatting

To conditionally format specific values, for example, display all negative values in Italics, set the Font.FontStyle property to the following expression.

=IIF(Fields!FieldName.Value<0,"Italic","Normal")

Show only on the specified page 

To set a field to show only on the final page of the report, in the Properties Window, find the control's Value property and click the drop-down arrow button to open the expression editor and enter an expression like the following.

=IIF(Globals!PageNumber=Globals!TotalPages,
Fields!FieldName.Value,"")


For more information about expressions and examples of Page and RDL reports, visit the following topics in our User Guide:

Expressions | Create Red Negatives Report | Create Green Bar Report
Expressions in Reports | Reports with Custom Code