ActiveReports 18 .NET Edition
Developers / Working with Reports / Page/RDLX Report / Add Code to Layouts Using Script
In This Topic
    Add Code to Layouts Using Script
    In This Topic

    In a Page Report or an RDLX Report, you can use custom code in your expressions to extend the capabilities of your report. However, for complex functions, or functions you plan to use many times in the report, you also have the facility to embed the code within the report. You can also create and maintain a custom assembly for code that you want to use in multiple reports and refer to its methods in expressions.

    Note:

    1. You should be aware that scripts allow to execute different code and can be a security gap. Therefore, it is not recommended to use scripts in web/cloud environments.
    2. In WebDesigner, the Scripts are available for Section reports only.

    To turn off scripts in WinForms Designer, use the following code:    

    For WinForms Designer
    Copy Code
    _designer.EnableScripting = false;
    

    Page/RDLX reports are designed to avoid scripts. However, some situations may still require using scripts. Page/RDLX reports support Visual Basic scripts, which can be used as expressions - see the following example:

    Script in Page/RDLX reports
    Copy Code
    Public Function GetRandom(ByVal seed As Int32, ByVal maxValue As Int32) As Int32
        Return New System.Random(seed).Next(maxValue)
    End Function
    

    Then, you should set a Textbox to a value as in the example below.

    Example Title
    Copy Code
    =Code.GetRandom(Fields!ID.Value, 10000)
    

    Embed Code in the Report

    Add a Page Report/RDLX report template to your project and in the Visual Studio Integrated Designer that appears, add code like the following in the Script tab.

    Script Tab

    Call a function from the control's property

    This is a simple example of a single method code block:

    Copy Code
    Public Function GetDueDate() as Date
         Return DateTime.Now.AddDays(30)
     End Function
    

    This is an expression used to call the method in the code block from the control's property. For example, you can call this function in the Value property of the Textbox control:

    Copy Code
    =Code.GetDueDate()
    

    Use the custom constant and variable from the control's property

    This is a simple example of how to define custom constants and variables in your code block:

    Copy Code
    Public Dim MyVersion As String = "123.456"
    Public Dim MyDoubleVersion As Double = 123.456
    Public Const MyConst As String = "444"
    

    This is an expression to use the custom constant and variable in the code block from the control's property. For example, you can get the value of a variable or a constant in the Value property of the Textbox control:

    Copy Code
    =Code.MyVersion
    =Code.MyDoubleVersion
    =Code.MyConst
    

    Call a global collection from the control's property

    This is a simple example of a global collection code block where the code block references the report object to access the report parameter value:

    Copy Code
    Public Function ReturnParam() As String
        Return "param value = " + Report.Parameters!ReportParameter1.value.ToString()
    End Function
    

    This is an expression used to call a global collection in the code block from the control's property. For example, you can call the global collection in the Value property of the Textbox control:

    Copy Code
    =Code.ReturnParam()
    

    Use instance-based Visual Basic .NET code in the form of a code block. You can include multiple methods in your code block, and access those methods from expressions in the control properties.

    Note: In a Page Report or an RDLX Report, you can use Visual Basic.NET as the script language. However, you can use both Visual Basic.Net and C# in your script for a Section Report.

    Create custom assemblies

    You can create custom assemblies in C# or Visual Basic .NET to make code available to multiple reports:

    1. Create or find the assembly you wish to use. The assemblies can be found in the installed location: C:\Program Files (x86)\MESCIUS\ActiveReports 18\NuGet\{Package name}\lib\net46\{Assembly}
    2. Make the assembly available to the report engine.
      • If you are embedding the designer or viewer controls in your own application, copy the assembly to the same location as your executable.
      • If you are using the included designer or viewer, copy the assembly into the ActiveReports assembly folder located at ...\MESCIUS\ActiveReports 18 by default.
        Note: To make the assembly available for use in your own application and for use in designing reports for your application, copy it to both locations listed above. Alternatively, you can place the assembly in the Global Assembly Cache (C:\Windows\assembly).
    3. Add an assembly reference to the report.
      1. From the Report Menu, choose Report Properties.
      2. In the Report dialog that appears, select the References and click the Open icon above the assembly name list to add your own assembly.
      3. Go to the Class list below the assembly names and under Class name, enter the namespace and class name. Similarly, under Instance name, enter the name you want to use in your expressions.

    4. Access the assembly through expressions.
      • To access static members (member denoted as public static in C# assemblies, or as Public Shared in Visual Basic assemblies):
        =Namespace.Class.Member
      • To access class instances:
        =Code.InstanceName