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. |
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. |
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 add script to a report to access a textbox named TextBox1 in the detail section and assign the text "Hello" to it:
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
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" |
To 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"; |
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.
To 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 |
To create a public function in C#
C# code. Paste INSIDE the new class. |
Copy Code |
---|---|
public string getMyItem() { return "Hello"; } |
To access the class in VB.NET
VB.NET code. Paste before or in the ReportStart event. |
Copy Code |
---|---|
Me.AddNamedItem("myItem", new clsMyItem()) |
To 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. |
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
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() |
To 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() |
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
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 |
To access an assembly 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; } |
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
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) |
To 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". |
To 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() |
To 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(); |
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
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 |
To 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(); |