How to Change .NET C# Report Control Properties at Runtime
Tags: webdev, devops, tutorial, csharp
Published: 2024-05-29 00:58:19

---
canonical_url: https://developer.mescius.com/blogs/how-to-change-net-c-sharp-report-control-properties-at-runtime
description: Learn how to change .NET C# report control properties at runtime. See more from ActiveReports today.
---
**What You Will Need**
- ActiveReports.NET
- Visual Studio 2017 - 2022 for Code-Based Section Reports

**Controls Referenced**
- [Section Report Scenarios](https://developer.mescius.com/activereportsnet/docs/latest/online/tutorials-section-report-scenarios.html)
- [Expressions](https://developer.mescius.com/activereportsnet/docs/latest/online/expressions.html)
- [Code-based Section Reports in .NET Core](https://developer.mescius.com/activereportsnet/docs/latest/online/designing-code-based-section-reports-in-net-core.html)
- [Report Events](https://developer.mescius.com/activereportsnet/docs/latest/online/report-events.html)

**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:

*   [Page and RDL Reports](#Page)
*   [Code-Based Section Reports](#Code)
*   [XML-Based Section Reports](#XML)

![.NET C# Report](//cdn.mescius.io/umb/media/ikoanpgn/net-c-report.png?rmode=max&width=661&height=427)

## <a id="Page"></a>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](https://developer.mescius.com/activereportsnet/docs/latest/online/expressions.html "https://developer.mescius.com/activereportsnet/docs/latest/online/expressions.html") | [Create Red Negatives Report](https://developer.mescius.com/activereportsnet/docs/latest/online/create-red-negatives-greenbar-report.html "https://developer.mescius.com/activereportsnet/docs/latest/online/create-red-negatives-greenbar-report.html") | [Create Green Bar Report](https://developer.mescius.com/activereportsnet/docs/latest/online/create-red-negatives-greenbar-report.html "https://developer.mescius.com/activereportsnet/docs/latest/online/create-red-negatives-greenbar-report.html") |  [Expressions in Reports](https://developer.mescius.com/activereportsnet/docs/latest/online/expressions.html "https://developer.mescius.com/activereportsnet/docs/latest/online/expressions.html") | [Reports with Custom Code](https://developer.mescius.com/activereportsnet/docs/latest/online/designing-code-based-section-reports-in-net-core.html "https://developer.mescius.com/activereportsnet/docs/latest/online/designing-code-based-section-reports-in-net-core.html")

## <a id="Code"></a>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](https://developer.mescius.com/activereportsnet/docs/latest/online/report-events.html "https://developer.mescius.com/activereportsnet/docs/latest/online/report-events.html") | [Conditional Scenarios](https://developer.mescius.com/activereportsnet/docs/latest/online/tutorials-section-report-scenarios.html "https://developer.mescius.com/activereportsnet/docs/latest/online/tutorials-section-report-scenarios.html") | [Create Green Bar Report](https://developer.mescius.com/activereportsnet/docs/latest/online/create-green-bar-reports.html "https://developer.mescius.com/activereportsnet/docs/latest/online/create-green-bar-reports.html")

## <a id="XML"></a>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.