If you are developing applications which includes 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 latter 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 viewers’ print buttons’ print action.
Create a new windows application.
Add another form to the project and name it as ‘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)
Add new report to the project and name it as ‘SampleReport.rpt’.
Now add the following code to the ‘CustomReportViewer’ class
public delegate void CustomPrintDelegate();
Add the following property.
public Delegate CustomPrintMethod { get; set; }
Add this additional code to the initialization method.
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 coding we can find out the print button of the report viewers’ too strip. And the 1st item is for the print button. (I have found this out from its ToolTipText).
How ever 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.
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 coding of the ‘CustomReportViewer’ class.
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
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.
private void CustomPrintMethod() {
MessageBox.Show("Custom Print Method");
}
And a button and the following click event code.
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:
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 similar screen shown below. And please note that I have added a resource file named ‘Resources’ and added an image named ‘CustomButton’
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.