ActiveReports 6 Online Help
Add Code to Layouts Using Script
Show AllShow All
Hide AllHide All

When you save an ActiveReport to Report XML (RPX) format, the RPX file contains the layout, but does not contain any of the Visual Basic or C# code that you may have added to the code behind the report. For this reason, ActiveReports provides a Script tab at the bottom of the report design window.

To access the script editor, click the script tab.

You can use scripting to provide VB.NET or C# functionality to reports without compiling .vb or .cs files. This permits reports saved to report RPX format to serve as stand-alone reports. By including scripting when you save the report as an RPX file, you can later load, run, and display it in the viewer control without the designer. This allows you to update distributed reports without recompiling. You can use Visual Basic or C# script.

ActiveReports loads RPX files, including any scripting, in the InitializeComponent() method.

You can add script to the script editor at design time, or use the rpt.Script property to add it at run time and save it to the RPX file.

Since the RPX file can be read with any text editor, use the AddCode or AddNamedItem method to add secure information, such as a connection string, to a project.

Note: The ActiveReports script editor supports IntelliSense that helps the writing of code by making the access to the language elements fast and easy.

Tips for Using Scripting

Note: The basic approach of using the "this/Me" and "rpt" keywords is as follows - use "this/Me" to access the properties and controls added to the sections of the report, whereas use "rpt" within the instance of the ActiveReports class only to access its public properties, public events and public methods.

To select the scripting language to use

  1. In design view of the report, click in the grey area below the report to select it.
  2. In the Properties window, drop down the ScriptLanguage property and select C# or VB.NET.

To use the Script view of the report

When you select the Script tab, there are two drop-down boxes at the top of the tab:

Add script to the events in the same way that you add code to events in the code view of the report.

To access controls in script

To add script to a report to access a textbox named TextBox1 in the detail section and assign the text "Hello" to it:

  1. On the script tab of the report, drop down the Object list and select Detail1. This populates the Event drop-down list with section events.
  2. Drop down the Event list and select Format. This creates script stubs for the event.
    Note: Use the examples with the "this" and "Me" keywords, as they are recommended rather than the ones with "rpt".

    To access a textbox in the detail section in VB.NET script

    To access a textbox in the detail section in C# script

To give a script access to functions in a class in your project

Use the AddNamedItem method to allow the script to access functions in a class file within your project. This allows you to keep secure information such as a database connection string or a SQL query string in the code instead of in the RPX file.

  1. Add a class to your project named clsMyItem.
  2. Add a public function to your class using code like the following:

    To create a public function in Visual Basic.NET

    To create a public function in C#

  3. Open the ReportStart event handler (code-behind) and add the following code:

    To access the class in VB.NET

    To access the class in C#

    You can also use the AddNamedItem method before the report Run method.

  4. On the script tab of the report, drop down the Object list and select Detail1. This populates the Event drop-down list with section events.
  5. Drop down the Event list and select Format. This creates script stubs for the event.
  6. Add script to the event to typecast a control on the report and populate it using the named item.
    Note: Use the examples with the "this" and "Me" keywords, as they are recommended rather than the ones with "rpt".

    To typecast the control in VB.NET script

    To typecast the control in C# script

To access assemblies

Use the AddScriptReference method to gain access to .NET or other assemblies. This is only necessary if you need a reference, such as System.Data.dll, that is not initialized in the project before the script runs.

To access an assembly in Visual Basic.NET

To access an assembly in C#

To add code to a report's script from a Windows Form

Use the AddCode method to inject code into the script.

The AddCode method allows you to add actual code segments to the script at run time. This is useful for allowing secure information, such as a database connection string or SQL query string, to be used inside the script without saving it into the RPX file.

To add code in Visual Basic.NET

To add code in C#

Note: Use the examples with the "this" and "Me" keywords, as they are recommended rather than the ones with "rpt".

To write the script in Visual Basic.NET

To write the script in C#

To create classes inside the script to call methods

If the script requires method calls, you can construct a class inside the script.

Note: Use the examples with the "this" and "Me" keywords, as they are recommended rather than the ones with "rpt".

To create a class inside the script in VB.NET script

To create a class inside the script in C#

See Also

Walkthroughs