Skip to main content Skip to footer

How to Change .NET C# Report Control Properties at Runtime

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 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. For example, the code changes the text property of a Label in C# and VB.NET:

C#:

private void detail_Format(object sender, EventArgs e)

{

  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);

}

VB.NET:

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

(**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 | Conditional Scenarios | 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 the script to report events in the script tab. For example, the code changes the text property of a Label in C# and VB.NET:

C#: 

public void Detail_Format()  

{ 

  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);  

}

VB.NET:

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

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 a WYSIWYG output, 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 select <Expression...>. With the Expression Editor open, 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 Specific Page

To set a field to show only on the report's final page, in the Properties Window, find the control's Value property and click the drop-down arrow to select <Expression...>. With the Expression Editor open, 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 ReportExpressions in Reports | Reports with Custom Code

 

There is a lot more to dynamic reporting with ActiveReports. Download the latest version to test these features and more for yourself.

comments powered by Disqus