ActiveReports 9 > ActiveReports User Guide > How To > Section Report How To > Add Code to Layouts Using Script |
In a section report, you can use script to access controls, functions in a class, namespaces, etc. You can also create classes inside the script to call methods or add code to a report's script from a Windows Form. The following sections illustrate simple scripting scenarios with examples.
These steps assume that you have already added a Section Report (code based) template in a Visual Studio project. See Adding an ActiveReport to a Project for more information.
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:
To access a textbox in the detail section in VB.NET script
Visual Basic.NET script. Paste INSIDE the Detail Format event. |
Copy Code
|
---|---|
Me.TextBox1.Text = "Hello" |
Or
Visual Basic.NET script. 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. Paste INSIDE the Detail Format event. |
Copy Code
|
---|---|
this.textBox1.Text = "Hello"; |
Or
C# script. Paste INSIDE the Detail Format event. |
Copy Code
|
---|---|
((TextBox)rpt.Sections["detail"].Controls["TextBox1"]).Text = "Hello"; |
To give a script access to functions in a class in your project
Using the AddNamedItem method, you can 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 saving it in the RPX file.
To add a class in Visual Basic.NET
Visual Basic.NET code. |
Copy Code
|
---|---|
Public Class clsMyItem End Class |
To add a class in C#
C# code. |
Copy Code
|
---|---|
public partial class clsMyItem { } |
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 Visual Basic.NET
Visual Basic.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()); |
To access the control in VB.NET script
VB.NET script. Paste INSIDE the Detail Format event. |
Copy Code
|
---|---|
Me.TextBox1.Text = myItem.getMyItem() |
Or
VB.NET script. Paste INSIDE the Detail Format event. |
Copy Code
|
---|---|
CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = myItem.getMyItem() |
To access the control in C# script
C# script. Paste INSIDE the Detail Format event. |
Copy Code
|
---|---|
this.textBox1.Text = myItem.getMyItem(); |
Or
C# script. Paste INSIDE the Detail Format event. |
Copy Code
|
---|---|
((TextBox)rpt.Sections["detail"].Controls["textBox1"]).Text = myItem.getMyItem(); |
To access namespaces
Using the AddScriptReference method, you can 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.
To access a namespace in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the Form code. Replace YourReportName with the name of your report. |
Copy Code
|
---|---|
Private Sub runReport() Dim rpt as new YourReportName rpt.AddScriptReference("System.Data.dll") rpt.Run() End Sub |
To access a namespace in C#
C# code. Paste INSIDE the Form code. Replace YourReportName with the name of your report. |
Copy Code
|
---|---|
private void runReport() { YourReportName rpt = new YourReportName; rpt.AddScriptReference("System.Data.dll"); rpt.Run(); } |
To add code to a report's script from a Windows Form
Using the AddCode method in the Code View of the Form, you can add 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 in 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 |
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; } |
To access the class in Visual Basic.NET
Visual Basic.NET code. Paste INSIDE the ReportStart event. |
Copy Code
|
---|---|
Me.AddCode(addThisCode())
|
To access the class in C#
C# code. Paste INSIDE the ReportStart event. |
Copy Code
|
---|---|
this.AddCode(addThisCode()); |
To write the script in Visual Basic.NET
VB.NET script. Paste INSIDE the Detail1_Format event. |
Copy Code
|
---|---|
Me.TextBox1.Text = ShowACMessage() |
Or
VB.NET script. Paste INSIDE the Detail1_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(); |
Or
C# script. Paste INSIDE the detail_Format event. |
Copy Code
|
---|---|
((TextBox)rpt.Sections["detail"].Controls["textBox1"]).Text = ShowACMessage(); |
To create classes inside the script to call methods
If the script requires a method to be called, you can construct a class inside the script.
To create a class inside the script in VB.NET script
VB.NET script. Paste INSIDE the script tab. |
Copy Code
|
---|---|
Public Class MyFuncs Public Sub New() End Sub Public Function ShowMyString() As String Return "This is my string" End Function End Class |
To create a class inside the script in C#
C# script. Paste INSIDE the script tab. |
Copy Code
|
---|---|
public class MyFuncs { public MyFuncs() { } public string ShowMyString() { return "This is my string"; } } |
To create a class inside the script in VB.NET script
VB.NET script. Paste INSIDE the Detail1_Format event. |
Copy Code
|
---|---|
Dim f As MyFuncs = New MyFuncs() Me.TextBox1.Text = f.ShowMyString |
Or
VB.NET script. Paste INSIDE the Detail1_Format event. |
Copy Code
|
---|---|
Dim f As MyFuncs = New MyFuncs() CType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = f.ShowMyString |
To create a class inside the script in C#
C# script. Paste INSIDE the detail_Format event. |
Copy Code
|
---|---|
MyFuncs f = new MyFuncs(); this.textBox1.Text = f.ShowMyString(); |
Or
C# script. Paste INSIDE the detail_Format event. |
Copy Code
|
---|---|
MyFuncs f = new MyFuncs(); ((TextBox)rpt.Sections["detail"].Controls["textBox1"]).Text = f.ShowMyString(); |
Note: Use the examples with the "this" (C#) and "Me"(Visual Basic.NET) keywords, as they are recommended rather than the ones with "rpt". |