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 add script to a report to access a textbox named TextBox1 in the detail section and assign the text "Hello" to it:
- On the script tab of the report, drop down the Object list and select Detail. This populates the Event drop-down list with section events.
- Drop down the Event list and select Format. This creates script stubs for the event.
To access a textbox in the detail section in VB.NET scriptVisual Basic.NET script. Paste INSIDE the Detail Format event.
Copy CodeMe.TextBox1.Text = "Hello" Or
Visual Basic.NET script. Paste INSIDE the Detail Format event.
Copy CodeCType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = "Hello"
To access a textbox in the detail section in C# scriptC# script. Paste INSIDE the Detail Format event.
Copy Codethis.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.
- In the Code View of the Form, add a class to your project named clsMyItem.
To add a class in Visual Basic.NETVisual Basic.NET code.
Copy CodePublic Class clsMyItem End Class
C# code.
Copy Codepublic partial class clsMyItem { } - Add a public function to your class using code like the following:
To create a public function in Visual Basic.NETVisual Basic.NET code. Paste INSIDE the new class.
Copy CodePublic Function getMyItem() As String getMyItem = "Hello" End Function
To create a public function in C#C# code. Paste INSIDE the new class.
Copy Codepublic string getMyItem() { return "Hello"; } - Go to the design view of the report and double-click the gray area around the design surface to create an event-handling method for the ReportStart event.
- Add the following code to the handler:
To access the class in Visual Basic.NETVisual Basic.NET code. Paste before or in the ReportStart event.
Copy CodeMe.AddNamedItem("myItem", new clsMyItem()) C# code. Paste before or in the ReportStart event.
Copy Codethis.AddNamedItem("myItem", new clsMyItem());
- From the Visual Studio toolbox, drag and drop a TextBox control onto the detail section of the report.
- Go to the script tab and drop down the Object list to select Detail. This populates the Event drop-down list with section events.
- Drop down the Event list and select Format. This creates script stubs for the event.
- Add the following script to the event to access a control on the report and populate it using the named item.
To access the control in VB.NET scriptVB.NET script. Paste INSIDE the Detail Format event.
Copy CodeMe.TextBox1.Text = myItem.getMyItem()
Or
VB.NET script. Paste INSIDE the Detail Format event.
Copy CodeCType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = myItem.getMyItem()
To access the control in C# scriptC# script. Paste INSIDE the Detail Format event.
Copy Codethis.textBox1.Text = myItem.getMyItem();
Or
C# script. Paste INSIDE the Detail Format event.
Copy Code((TextBox)rpt.Sections["detail"].Controls["textBox1"]).Text = myItem.getMyItem(); - Go to the preview tab to view the result.
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
|
|
| 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.
- Go to the Code View of your report and add a public function like the following:
To add code in Visual Basic.NETVisual Basic.NET code. Paste INSIDE the report class.
Copy CodePublic 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
C# code. Paste INSIDE the report class.
Copy Codepublic string addThisCode() { string sCode = "public string ShowACMessage(){return \"my Added Code\";}"; return sCode; } - In the design view of your report double-click the gray area around the design surface to create an event-handling method for the ReportStart event.
- Add the following code to the handler:
To access the class in Visual Basic.NETVisual Basic.NET code. Paste INSIDE the ReportStart event.
Copy CodeMe.AddCode(addThisCode())C# code. Paste INSIDE the ReportStart event.
Copy Codethis.AddCode(addThisCode()); - Go to the script tab and drop down the Object list to select Detail. This populates the Event drop-down list with section events.
- Drop down the Event list and select Format. This creates script stubs for the event.
- Add the following script to the event:
To write the script in Visual Basic.NETVB.NET script. Paste INSIDE the Detail1_Format event.
Copy CodeMe.TextBox1.Text = ShowACMessage() Or
VB.NET script. Paste INSIDE the Detail1_Format event.
Copy CodeCType(rpt.Sections("Detail1").Controls("TextBox1"), TextBox).Text = ShowACMessage() C# script. Paste INSIDE the detail_Format event.
Copy Codethis.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.
- Go to the script tab and add the following code at the top:
To create a class inside the script in VB.NET scriptVB.NET script. Paste INSIDE the script tab.
Copy CodePublic 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 Codepublic class MyFuncs { public MyFuncs() { } public string ShowMyString() { return "This is my string"; } } - On the script tab, now drop down the Object list and select Detail. This populates the Event drop-down list with section events.
- Drop down the Event list and select Format. This creates script stubs for the event.
- Add the following script to the event:
To create a class inside the script in VB.NET scriptVB.NET script. Paste INSIDE the Detail1_Format event.
Copy CodeDim f As MyFuncs = New MyFuncs() Me.TextBox1.Text = f.ShowMyString
Or
VB.NET script. Paste INSIDE the Detail1_Format event.
Copy CodeDim 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 CodeMyFuncs f = new MyFuncs(); this.textBox1.Text = f.ShowMyString();
Or
C# script. Paste INSIDE the detail_Format event.
Copy CodeMyFuncs 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". |
Concepts
Scripting in Section Reports
Hide All