Skip to main content Skip to footer

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

Quick Start Guide
What You Will Need

ActiveReports.NET

Visual Studio 2017 - 2022 for Code-Based Section Reports

Controls Referenced

Section Report Scenarios

Expressions

Code-based Section Reports in .NET Core

Report Events

Tutorial Concept Learn how to dynamically adjust properties of .NET C# report controls in real-time. Explore efficient methods for modifying control properties to enhance the functionality and aesthetics of your reports effortlessly.

When it comes to dynamically altering control properties at runtime in reports, the approach can vary depending on the type of report being used - whether it's a Section-based report or a Page/RDL-based report. In this article, we will look at the procedure for each of the following report types:

Ready to Get Started? Download ActiveReports.NET Today!

.NET C# Report

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, find the control's Value property in the Properties Window 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, displaying 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, find the control's Value property in the Properties Window 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 Report | Expressions in Reports | Reports with Custom Code

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 following 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 you cannot directly change the value of Font.Name. To change the Font property at run time, you must 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, PageHeader_BeforePrint, or GroupFooter_AfterPrint) or within the ReportStart event. You can also change properties at the report instance creation level in code outside 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 following 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

Conclusion

In summary, while both Section-based and Page/RDL-based reports offer ways to change control properties dynamically at runtime, the methods and techniques may vary depending on the underlying structure and features of the reporting tool being used. Understanding the capabilities and best practices for each type of report can help developers create more flexible and interactive reporting solutions.

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

Ready to Get Started? Download ActiveReports.NET Today!

Tags:

comments powered by Disqus