ActiveReports 14
ActiveReports 14 User Guide / How To / Section Report How To / Common Tasks / Add Code to Layouts Using Script
In This Topic
    Add Code to Layouts Using Script
    In This Topic

    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:

    1. 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.
    2. 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 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.

    1. In the Code View of the Form, add a class to your project named clsMyItem.

      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
      {
      }
      
    2. Add a public function to your class using code like the following:

      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"; 
      }
      
    3. 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.
    4. Add the following code to the handler:

      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());

    5. From the Visual Studio toolbox, drag and drop a TextBox control onto the detail section of the report.
    6. Go to the script tab and drop down the Object list to select Detail. This populates the Event drop-down list with section events.
    7. Drop down the Event list and select Format. This creates script stubs for the event.
    8. 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 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();
    9. Go to the preview tab to view the result.

    To access namespaces

    By using the AddScriptReference method, you can gain access to .NET or custom 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(); 
    }
    

    Note: If you are using the custom assemblies, they must have Strong Name and registered to GAC folder.

    If you want to use custom assemblies in the Script section of the Designer, then you need to add the assembly reference to a report before loading it into the report designer. To do this, follow these steps:

    1. Create a custom assembly with the strong name and register it to GAC.
    2. Create a new sample with a section report (XML-based).
    3. Add the report designer to the form.
    4. Add the following code to the Form_Load event.

      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
      Dim rpt As SectionReport = New SectionReport()
      Dim xtr As System.Xml.XmlTextReader = New System.Xml.XmlTextReader("..\..\SectionReport2.rpx")
      rpt.LoadLayout(xtr)
      xtr.Close()
      rpt.AddScriptReference("ClassLibrary1.dll")
      designer1.Report = rpt

      To access a namespace in C#

      C# code. Paste INSIDE the Form code. Replace YourReportName with the name of your report.
      Copy Code
      SectionReport rpt = new SectionReport(); 
      System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(@"..\..\SectionReport2.rpx");
      rpt.LoadLayout(xtr);
      xtr.Close();
      rpt.AddScriptReference(@"ClassLibrary1.dll");
      designer1.Report = rpt;
    5. Run the sample.
    6. Open the Script section of the designer.

    You can now use the custom assemblies reference in the Script section of the Desginer.

    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.

    1. Go to the Code View of your report and add a public function like the following:

      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; 
      } 
      
    2. 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.
    3. Add the following code to the handler:

      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());
    4. Go to the script tab and drop down the Object list to select Detail. 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 the following script to the event:

      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.

    1. Go to the script tab and add the following code at the top:

      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";
         }
      }
      
    2. On the script tab, now drop down the Object list and select Detail. This populates the Event drop-down list with section events.
    3. Drop down the Event list and select Format. This creates script stubs for the event.
    4. Add the following script to the event:

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