Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#

Invoke a Custom Method when Crystal Report Viewers’ Print Button is Clicked / Add Custom Button to Crystal Report Viewer Toolbar

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
8 Aug 2011CPOL2 min read 48.7K   7   4
How to invoke a method in our client application when the print button of the report viewer is clicked

If you are developing applications which include reporting with Crystal Reports, you may have noticed that it’s not possible to invoke a custom method when the user prints the report. However, this was something which was easily implemented in Crystal Reports 8/8.5 but removed from later versions.

But there’s a workaround for this. In this example, I will show you how to invoke a method in our client application when the print button of the report viewer is clicked.

In order to do that, we have to add our custom method to the report viewer’s print button’s print action.

Create a new Windows application. Add another form to the project and name it CustomReportViewer.cs.

Add a Crystal Report viewer to a newly created form. (If the Crystal Report Viewer is not available in the toolbox, please add it to the toolbox first.)

img_scr_001_a

img_scr_002

Add a new report to the project and name it SampleReport.rpt.

img_scr_003

Now add the following code to the CustomReportViewer class:

C#
public delegate void CustomPrintDelegate();

Add the following property:

C#
public Delegate CustomPrintMethod { get; set; }

Add this additional code to the initialization method:

C#
foreach (Control control in crystalReportViewer1.Controls) {
    if (control is System.Windows.Forms.ToolStrip) {

        //Default Print Button
        ToolStripItem tsItem = ((ToolStrip)control).Items[1];
        tsItem.Click += new EventHandler(tsItem_Click);
        
        //Custom Button
        ToolStripItem tsNewItem = ((ToolStrip)control).Items.Add("");
        tsNewItem.ToolTipText = "Custom Print Button";
        tsNewItem.Image = Resources.CustomButton;
        tsNewItem.Tag = "99";
        ((ToolStrip)control).Items.Insert(0, tsNewItem);
        tsNewItem.Click += new EventHandler(tsNewItem_Click);
    }
}

Using the above code, we can find out the print button of the report viewer’s tool strip. And the first item is for the print button. (I found this out from its ToolTipText).

However, you can add your own button if you like or you can use the existing print button. Both options are illustrated.

Add the following methods:

C#
void tsNewItem_Click(object sender, EventArgs e) {
    if (CustomPrintMethod != null) {
        CustomPrintMethod.DynamicInvoke(null);
    }
}

void tsItem_Click(object sender, EventArgs e) {
    if (CustomPrintMethod != null) {
        CustomPrintMethod.DynamicInvoke(null);
    }
}

Here is the complete code for the CustomReportViewer class:

C#
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace PrintDelegateMethod {
    public partial class CustomReportViewer : Form {

        public delegate void CustomPrintDelegate();

        public Delegate CustomPrintMethod { get; set; }

        public CustomReportViewer() {
            InitializeComponent();

            foreach (Control control in crystalReportViewer1.Controls) {
                if (control is System.Windows.Forms.ToolStrip) {

                    //Default Print Button
                    ToolStripItem tsItem = ((ToolStrip)control).Items[1];
                    tsItem.Click += new EventHandler(tsItem_Click);
                    
                    //Custom Button
                    ToolStripItem tsNewItem = ((ToolStrip)control).Items.Add("");
                    tsNewItem.ToolTipText = "Custom Print Button";
                    tsNewItem.Image = Resources.CustomButton;
                    tsNewItem.Tag = "99";
                    ((ToolStrip)control).Items.Insert(0, tsNewItem);
                    tsNewItem.Click += new EventHandler(tsNewItem_Click);
                }
            }
        }

        void tsNewItem_Click(object sender, EventArgs e) {
            if (CustomPrintMethod != null) {
                CustomPrintMethod.DynamicInvoke(null);
            }
        }

        void tsItem_Click(object sender, EventArgs e) {
            if (CustomPrintMethod != null) {
                CustomPrintMethod.DynamicInvoke(null);
            }
        }

        private void CustomReportViewer_Load(object sender, EventArgs e) {
            SampleReport report = new SampleReport();
            crystalReportViewer1.ReportSource = report;
            crystalReportViewer1.Refresh();
        }
    }
}

Add the following delegate to your calling class:

C#
public delegate void PrintDelegate();

Add the following method. This is the method that we want to invoke when the print button or the custom button is clicked.

C#
private void CustomPrintMethod() {
    MessageBox.Show("Custom Print Method");
}

And a button and the following click event code:

C#
private void button1_Click(object sender, EventArgs e) {
    CustomReportViewer viewer = new CustomReportViewer();
    PrintDelegate mymethod = new PrintDelegate(CustomPrintMethod);
    viewer.CustomPrintMethod = mymethod;
    viewer.Show();
}

The complete source of the calling form is as follows:

C#
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PrintDelegateMethod {
    public partial class Form1 : Form {
        public delegate void PrintDelegate();

        public Form1() {
            InitializeComponent();
        }

        private void CustomPrintMethod() {
            MessageBox.Show("Custom Print Method");
        }

        private void button1_Click(object sender, EventArgs e) {
            CustomReportViewer viewer = new CustomReportViewer();
            PrintDelegate mymethod = new PrintDelegate(CustomPrintMethod);
            viewer.CustomPrintMethod = mymethod;
            viewer.Show();
        }
    }
}

Now if you run the project, you can get a screen similar to the one shown below. And please note that I have added a resource file named ‘Resources’ and added an image named ‘CustomButton’.

img_scr_011_a

And if you click either of the buttons, your custom method will be invoked. The default print method will be executed only when the print button is clicked.

img_scr_012

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Air Liquide Industrial Services (Singapore)
Singapore Singapore
My passion lies in building business intelligence and data-based solutions, writing about things I work with and talking about it. New technologies relevant to my line of work interest me and I am often seen playing with early releases of such technologies.

My current role involves architecting and building a variety of data solutions, providing database maintenance and administration support, building the organization’s data practice, and training and mentoring peers.

My aspiration over the next several years is to achieve higher competency and recognition in the field of Data Analytics and move into a career of data science.


Specialities: SQL Server, T-SQL Development, SQL Server Administration, SSRS, SSIS, C#, ASP.Net, Crystal Reports

Comments and Discussions

 
QuestionMessage Closed Pin
24-Jul-12 11:08
Waqasswaleh24-Jul-12 11:08 
AnswerRe: Print Report without preview Pin
Manjuke Fernando24-Jul-12 14:32
professionalManjuke Fernando24-Jul-12 14:32 
Thanks..
Manjuke Fernando

GeneralRe: Print Report without preview Pin
Manju MJS9-Oct-12 0:07
Manju MJS9-Oct-12 0:07 
GeneralMy vote of 4 Pin
Member 856396423-Feb-12 1:20
Member 856396423-Feb-12 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.