ActiveReports for .NET 3 Online Help Request technical support
Using Inheritance
User Guide > How-To Section > Using Inheritance

Glossary Item Box

ActiveReports can be used to create a base class as a template from which other reports can inherit. This is useful when working with reports that share common features, such as identical page headers and footers. By using inheritance, you can create these shared headers and footers once instead of recreating the look every time. 

In ActiveReports, you can utilize inheritance at both design time and run time. A very simple example of how this functionality may be used is the creation of a company letterhead template.   

Creating a company letterhead template at Design Time

This example creates two reports, one of which serves as the base report and the other which serves as the derived report. To use the inheritance functionality of ActiveReports at design time create a new Visual Studio project and complete the following steps.

  1. Add an ActiveReport to your Visual Studio project and rename the file rptLetterhead. This report will serve as the base report.
  2. In the Properties window, set the MasterReport property to true.

  3. The detail section of the report is grayed out and cannot be used. Each derived report supplies its own detail section. 

  4. Add a Picture control from the ActiveReports 3.0 toolbox to the PageHeader section of rptLetterhead and make the following property changes to the Picture control.
    • Change the Name property to pictureLogo
    • Change the Image property by adding an appropriate image from file
    • Change the Location property to 0, 0
    • Change the Width property to 2.13
    • Change the Height property to 0.56
  5. Add a Label control from the ActiveReports 3.0 toolbox to the PageHeader section of rptLetterhead and make the following property changes to the Label control.
    • Change the Name property to lblCompanyTagline
    • Change the Location property to 0.75, 0.625
    • Change the Text property to Control Yourself!
    • Change the FontStyle to Bold
    • Change the Width property to 1.19
    • Change the Height property to 0.19
  6. Add a Label control from the ActiveReports 3.0 toolbox to the PageFooter section of rptLetterhead and make the following property changes to the Label control.
    • Change the Name property to lblWebsite
    • Change the Location property to 2.25, 0.06
    • Change the Text property to http://www.datadynamics.com
    • Change the FontStyle to Bold
    • Change the HyperLink property to http://www.datadynamics.com
    • Change the Width property to 2.19
  7. Add a second ActiveReport to your project and name the file rptLetter. This report is the derived report and inherits its PageHeader and PageFooter sections from rptLetterhead.
  8. Right-click on the design surface of rptLetter and click "View Code" to go to the code-behind of rptLetter.
  9. In the code-behind of rptLetter change the inheritance statement so that rptLetter inherits from rptLetterhead instead of DataDynamics.ActiveReports.ActiveReport3.

    The code looks like the following statement.

    //C# 
    public class rptLetter : <your project name>.rptLetterhead  
    
    'Visual Basic
    Public Class rptLetter
        Inherits<your project name>.rptLetterhead
    

    For Visual Basic projects created with VS2005 change the inheritance statement in the code-behind of the rpt.Designer file and add code similar to the following.

    'Visual Basic
    Partial Public Class rptLetter
        Inherits <your project name>.rptLetterhead

    For C# projects created with VS2005 the code looks like the following.

    public partial class rptLetter : <your project name>.rptLetterhead
  10. Rebuild your project. In the derived report, the design-time view shows the inherited sections and controls grayed out, indicating that they cannot be altered within the derived report. To make changes in these sections, you must return to the base report, make the alterations, and rebuild your project again.

As a best practice, it is recommended that base and derived reports not contain controls with duplicate names. The report can be compiled and run, but the duplicate names must be changed before the layout can be saved.

Creating a company letterhead template at Run Time

You can also create the same inheritance effect as shown above at run time. To do this, create a LetterHead class with code like the following. 

//C#
using DataDynamics.ActiveReports;
using DataDynamics.ActiveReports.Document;
namespace InheritanceCS
{
   /// <summary>
   /// Summary description for LetterHead.
   /// The LetterHead class is to be used with reports having only a Detail section.
   /// </summary>
   public class LetterHead : DataDynamics.ActiveReports.ActiveReport3 //Tell the letterhead class to inherit from ActiveReports
   {
        public LetterHead()
        {
         //
         // TODO: Add constructor logic here
         //
         }
         protected void InitializeLetterhead()
         {
            //create sections and controls for the letterhead
            PageHeader ph = new PageHeader();
            PageFooter pf = new PageFooter();
            Picture pictureLogo = new Picture();
            Label lblCompanyTagline = new Label();
            Label lblWebsite = new Label();
            
            //set properties on sections and controls
            ph.Height = 1f;
            pf.Height = 1f;
            
            //pictureLogo
	   System.Drawing.PointF dp1 = new System.Drawing.PointF(0f,0f);
	   pictureLogo.Image = System.Drawing.Image.FromFile(@"C:\DD_LogoSQ.jpg"); //insert your own image
	   pictureLogo.Width = 2.13f;
	   pictureLogo.Height = 0.56f;
	   pictureLogo.Location = dp1;
	
	   //lblCompanyTagline
	   System.Drawing.PointF dp2 = new System.Drawing.PointF(0.75f,0.625f);
	   lblCompanyTagline.Text = "Control Yourself!";
	   lblCompanyTagline.Width = 1.19f;
	   lblCompanyTagline.Height = 0.19f;
	   lblCompanyTagline.Location = dp2;
	   lblCompanyTagline.Font = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold);
			
	   //lblWebsite
	   System.Drawing.PointF dp3 = new System.Drawing.PointF(2.25f,0.06f);
	   lblWebsite.Text = "http://www.datadynamics.com";
	   lblWebsite.HyperLink = "http://www.datadynamics.com";
	   lblWebsite.Location = dp3;
	   lblWebsite.Width = 2.19f;
			
	   //add the sections created above to the report
	   this.Sections.Insert(0,ph);
	   this.Sections.Insert(2,pf);

	   //add the controls created above to the sections
	   ph.Controls.Add(pictureLogo);
	   ph.Controls.Add(lblCompanyTagline);
	   pf.Controls.Add(lblWebsite);
          }
          
          protected void rptLetter_ReportStart(object sender, System.EventArgs eArgs)
          {
             //add the code in InitializeLetterhead to the ReportStart event 
             //of the report
             InitializeLetterhead();
          }
     }
}

 

'Visual Basic
Imports System
Imports DataDynamics.ActiveReports
Imports DataDynamics.ActiveReports.Document
Namespace InheritanceVB
    Public Class Letterhead
        Inherits DataDynamics.ActiveReports.ActiveReport3 'Tell the letterhead class to inherit from ActiveReports
        Protected Sub InitializeLetterhead()
            'The Letterhead class is for use with reports containing only a Detail section.
            'create sections and controls for the letterhead
            Dim ph As PageHeader = New PageHeader
            Dim pf As PageFooter = New PageFooter
            Dim pictureLogo As Picture = New Picture
            Dim lblCompanyTagline As Label = New Label
            Dim lblWebsite As Label = New Label
            
            'set properties on sections and controls
            ph.Height = 1.0F
            pf.Height = 0.25F
            
            'pictureLogo
            Dim dp1 As New System.Drawing.PointF(0F,0F)
            With pictureLogo
                .Image = System.Drawing.Image.FromFile("c:\logo.gif") 'insert your own image
                .Width = 2.13F
                .Height = 0.56F
                .Location = dp1
            End With
            
            'lblCompanyTagline
            Dim dp2 As New System.Drawing.PointF(0.75, 0.625)
            With lblCompanyTagline
                .Text = "Control Yourself!"
                .Width = 1.19F
                .Height = 0.19F
                .Location = dp2
                .Font = New System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold)
            End With
            
            'lblWebsite
            Dim dp3 As New System.Drawing.PointF(2.25F, 0.06F)
            With lblWebsite
                .Text = "http://www.datadynamics.com"
                .HyperLink = "http://www.datadynamics.com"
                .Width = 2.19F
                .Location = dp3
            End With
            
            'add the sections created above to the report
            Me.Sections.Insert(0, ph)
            Me.Sections.Insert(2, pf)
            
            'add the controls created above to the sections
            ph.Controls.Add(pictureLogo)
            ph.Controls.Add(lblCompanyTagline)
            pf.Controls.Add(lblWebsite)
        End Sub
        
        Protected Sub rptLetter_ReportStart(ByVal sender As Object, ByVal eArgs As System _
        	.EventArgs)
            'add the code in InitializeLetterhead to the ReportStart event of the report
            InitializeLetterhead()
        End Sub
    End Class
End Namespace

Then in the code-behind of any report that is to inherit from this class, change the inheritance in the automatically generated code from "DataDynamics.ActiveReports.ActiveReport3" to your class name, in this case "LetterHead."

//C#
public class rptLetter < your project name>.LetterHead

'Visual Basic
Public Class <your project name>.InheritanceVB.LetterHead

For Visual Basic projects created with VS2005 change the inheritance statement in the code-behind of the rpt.Designer file and add code similar to the following.

'Visual Basic
Partial Public Class rptLetter
    Inherits <your project name>.LetterHead

For C# projects created with VS2005 the code looks like the following.

public partial class rptLetter : <your project name>.rptLetterhead

 

To use the code in the ReportStart event you created in the class above, create an event handler in the constructor of the report after InitializeComponent() with code like the following.

//C#
this.ReportStart += new EventHandler(base.rptLetter_ReportStart);

 

'Visual Basic
AddHandler Me.ReportStart, AddressOf rptLetter_ReportStart
    ©2009. All Rights Reserved.