SAP Knowledge Base Article - Public

1202817 - How to add a customized print button to the Crystal Reports viewer control in Visual Studio .NET Windows application instead of using existing print button?

Symptom

NOTE: The buttons can change ID's, this is not supported but for those who wish to can do as follows.

How to add a customized print button to the Crystal Reports viewer control in Visual Studio .NET 2008 Windows application instead of using existing print button?

Resolution

Customized print button cna be added to the Crystal Reports Viewer by using ToolStrip.

Please refer to the following code to add a customized print button to the Crystal Reports Viewer control:

-------------------------------------------------------------------------------------------------------------------------------------------------- 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Drawing.Printing;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        CrystalReport1 cr;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ToolStrip ts;
            ts = (ToolStrip)crystalReportViewer1 .Controls[3];

            ToolStripButton printbutton = new ToolStripButton();
            printbutton.Image = ts.Items[1].Image;
            ts.Items.Remove(ts.Items[1]);
            ts.Items.Insert(1, printbutton);

            ts.Items[1].Click += new EventHandler(this.CaptureEvent);                  
            cr = new CrystalReport1();
            this.crystalReportViewer1.ReportSource = cr;
                         
        }

        private void CaptureEvent(Object Sender, EventArgs e)
        {
            try
            {        // Create a Print Dialog                
                PrintDialog printDialog1 = new PrintDialog();

                // Create a Print Document
                PrintDocument pd = new PrintDocument();

                printDialog1.Document = pd;
                printDialog1.ShowNetwork = true;
                printDialog1.AllowSomePages = true;

                DialogResult result = printDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    MessageBox.Show("Print button has been selected !!!");
                    PrintReport(printDialog1.PrinterSettings.PrinterName);
                }
                else if (result == DialogResult.Cancel)
                {
                    MessageBox.Show("Cancel button has been selected !!!");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         }


        private void PrintReport(string printerName)
        {
            // Select the printer.
            cr.PrintOptions.PrinterName = printerName;

            /* Print the report. Set the startPageN and endPageN
             parameters to 0 to print all pages.*/
            cr.PrintToPrinter(1, false, 0, 0);
        }                           
    }
}
-------------------------------------------------------------------------------------------------------------------------------------------------- 
 
For a complete project see this KBA also:
1765153 - How to customize .NET Viewer Control Export button to manage custom export options and enable Application as the destination.

Keywords

Customized button using toolstrip net 2008 code print printer , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , How To

Product

Crystal Reports 2008 V0