ActiveReports 6 Online Help
Add Code to Layouts Using Script
See Also Send comments on this topic.
ActiveReports 6 > ActiveReports User Guide > How To > Add Code to Layouts Using Script

Glossary Item Box

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".

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

    Visual Basic.NET script. On the Script tab of the report, paste INSIDE the Detail Format event. Copy Code

    Me.TextBox1.Text = "Hello"

    Visual Basic.NET script. On the Script tab of the report, paste INSIDE the Detail Format event. Copy Code
    CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = "Hello"

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

    C# script. On the Script tab of the report, paste INSIDE the Detail Format event. Copy Code

    this.TextBox1.Text = "Hello";

    C# script. On the Script tab of the report, paste INSIDE the Detail Format event. Copy Code
    ((TextBox)rpt.Sections["detail1"].Controls["TextBox1"]).Text = "Hello";

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:

    ShowTo create a public function in Visual Basic.NET

    Visual Basic.NET code. Paste INSIDE the new class. Copy Code
    Public Function getMyItem() As String 
        getMyItem = "Hello" 
    End Function
    

    ShowTo create a public function in C#

    C# code. Paste INSIDE the new class. Copy Code
    public string getMyItem() 
    { 
        return "Hello"; 
    }
    
  3. Open the ReportStart event handler (code-behind) and add the following code:

    ShowTo access the class in VB.NET

    VB.NET code. Paste before or in the ReportStart event. Copy Code
    Me.AddNamedItem("myItem", new clsMyItem())

    ShowTo access the class in C#

    C# code. Paste before or in the ReportStart event. Copy Code

    this.AddNamedItem("myItem", new clsMyItem());

    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".

    ShowTo typecast the control in VB.NET script

    VB.NET script. Paste INSIDE the Detail Format event. Copy Code

    Me.textBox1.Text = myItem.getMyItem()

    VB.NET script. Paste INSIDE the Detail Format event. Copy Code
    CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = myItem.getMyItem()

    ShowTo typecast the control in C# script

    C# script. Paste INSIDE the Detail Format event. Copy Code

    this.textBox1.Text = myItem.getMyItem();

    C# script. Paste INSIDE the Detail Format event. Copy Code
    ((TextBox)rpt.Sections["detail1"].Controls["TextBox1"]).Text = myItem.getMyItem()

To access namespaces

Use the AddScriptReference method to gain access to .NET or other namespaces. 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.

ShowTo access a namespace in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the Form code. Copy Code
Private Sub runReport() 
      Dim rpt as new ActiveReport1() 
      rpt.AddScriptReference("System.Data.dll") 
      rpt.Run() 
      Me.Viewer1.Document = rpt.Document 
End Sub

ShowTo access a namespace in C#

C# code. Paste INSIDE the Form code. Copy Code
private void runReport() 
{ 
      ActiveReport1 rpt = new ActiveReport1(); 
      rpt.AddScriptReference("System.Data.dll"); 
      rpt.Run(); 
      this.viewer1.Document = rpt.Document; 
}

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.

ShowTo add code in Visual Basic.NET

Visual Basic.NET code. Paste INSIDE the report class. Copy Code
Public Function addThisCode() As String 
   Dim sCode As String = "Public Function ShowACMessage() As String" + Environment.NewLine + "ShowACMessage = ""my Added Code""" + Environment.NewLine + "End Function"
   addThisCode = sCode 
End Function 
VB.NET script. Paste INSIDE the ReportStart event. Copy Code
rpt.AddCode(addThisCode)

ShowTo add code in C#

C# code. Paste INSIDE the report class. Copy Code
public string addThisCode() 
{ 
   string sCode = "public string ShowACMessage()"{return \"my Added Code\";}"; 
   return sCode; 
} 
C# script. Paste INSIDE the ReportStart event. Copy Code
rpt.AddCode(addThisCode());
Note: Use the examples with the "this" and "Me" keywords, as they are recommended rather than the ones with "rpt".

ShowTo write the script in Visual Basic.NET

VB.NET script. Paste INSIDE the Detail Format event. Copy Code

Me.TextBox1.Text = ShowACMessage()

VB.NET script. Paste INSIDE the Detail Format event. Copy Code
CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = ShowACMessage()

ShowTo write the script in C#

C# script. Paste INSIDE the Detail Format event. Copy Code
this.TextBox1.Text = ShowACMessage();
C# script. Paste INSIDE the Detail Format event. Copy Code
((TextBox)rpt.Sections["Detail1"].Controls["TextBox1"]).Text = ShowACMessage();

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".

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

VB.NET script. Paste INSIDE the report class. Copy Code
Public Class MyFuncs
   Public  Sub New()
   End Sub
   Public Function ShowMyString() As String
      Return "This is my string"
   End Function
End Class
VB.NET script. Paste INSIDE the Detail Format event. Copy Code
Dim f As MyFuncs =  New MyFuncs()
Me.TextBox1.Text = f.ShowMyString
VB.NET script. Paste INSIDE the Detail Format event. Copy Code
Dim f As MyFuncs =  New MyFuncs() 
CType(rpt.Sections("Detail").Controls("TextBox1"), TextBox).Text = f.ShowMyString

ShowTo create a class inside the script in C#

C# script. Paste INSIDE the report class. Copy Code
public class MyFuncs
{
   public MyFuncs()
   {
   }
   public string ShowMyString()
   {
      return "This is my string";
   }
}
C# script. Paste INSIDE the Detail Format event. Copy Code
MyFuncs f = new MyFuncs();
this.TextBox1.Text = f.ShowMyString();
C# script. Paste INSIDE the Detail Format event. Copy Code
MyFuncs f = new MyFuncs();
((TextBox)rpt.Sections["Detail1"].Controls["TextBox1"]).Text = f.ShowMyString();

See Also