Показывать пользовательский диалог печати при нажатии кнопки печати

#c# #crystal-reports

#c# #crystal-отчеты

Вопрос:

Я создал свой собственный диалог печати в своем приложении C # для Windows. Я хочу показывать этот диалог, когда пользователь нажимает кнопку печати в Crystal Report viewer. Я не могу найти ни одного события нажатия кнопки печати в событиях CRViewer. Как я могу этого добиться?

Я хочу обновить некоторые изменения в базе данных во время печати отчета.

Ниже приведен мой код диалога печати;

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using JTQEducation.Winform.Common;
using JTQEducation.Common;

namespace JTQEducation.Winform
{
    public partial class frmPrintOptions : Form
    {
        const string MODULE_NAME = "frmPrintOption";
        bool m_blnCollated;
        int m_intNoofCoppies;
        int m_intFromPages;
        int m_intToPages;
        bool m_blnPrintAllPages;
        bool m_blnSelected;
        string m_strPrinterName;
        string m_strPrinterDriver;
        string m_strPrinterPort;
        bool m_blnPrintApplied;

        ReportDocument m_rptObject;

    public ReportDocument ReportObject
    {
        get { return m_rptObject; }
        set { m_rptObject = value; }
    }

    public string PrinterName
    {
        get { return m_strPrinterName; }
        set { m_strPrinterName = value; }
    }

    public string PrinterPort
    {
        get { return m_strPrinterPort; }
        set { m_strPrinterPort = value; }
    }

    public string PrinterDriver
    {
        get { return m_strPrinterDriver; }
        set { m_strPrinterDriver = value; }
    }

    public bool Selected
    {
        get { return m_blnSelected; }
        set { m_blnSelected = value; }
    }

    public bool PrintApplied
    {
        get { return m_blnPrintApplied; }
        set { m_blnPrintApplied = value; }
    }

    public int ToPages
    {
        get { return m_intToPages; }
        set { m_intToPages = value; }
    }

    public bool PrintAllPages
    {
        get { return m_blnPrintAllPages; }
        set { m_blnPrintAllPages = value; }
    }

    public bool Collated
    {
        get { return m_blnCollated; }
        set { m_blnCollated = value; }
    }

    public int NoofCopies
    {
        get { return m_intNoofCoppies; }
        set { m_intNoofCoppies = value; }
    }

    public int FromPages
    {
        get { return m_intFromPages; }
        set { m_intFromPages = value; }
    }

    public frmPrintOptions()
    {
        InitializeComponent();
    }

    private void frmPrintOptions_Load(object sender, EventArgs e)
    {
        try
        {
            //PrintDialog PD = new PrintDialog();


            //m_strPrinterDriver = PrinterDriverName;
            //m_strPrinterPort = PrinterPort;
            m_blnPrintApplied = false;
            foreach (string strPrinter in PrinterSettings.InstalledPrinters)
                {
                     cmbPrint.Items.Add(strPrinter);
                }
                cmbPrint.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            CommonHelper.RaiseError(ex.Message, MODULE_NAME,"frmPrintOption_Load");
        }
    }

    private bool ValidInput()
    {
        try
        {
            bool blnValidInput = true;

            if ((optPages.Checked) amp;amp; Convert.ToInt32(txtFrom.Text.Trim()) > Convert.ToInt32(txtTo.Text.Trim()))
            {
                MessageBox.Show("Page No. From is greater than Page No. To",
                    Constant.AppTitle, MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                blnValidInput = false;
            }

            if (optPages.Checked amp;amp; ((Convert.ToInt32(txtFrom.Text.ToString()) == 0) || (Convert.ToInt32(txtTo.Text.ToString()) == 0)))
            {
                MessageBox.Show("'From' or 'To' value is required.",
                    Constant.AppTitle, MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                blnValidInput = false;
            }

            if (Convert.ToInt32(txtCoppies.Text.Trim()) == 0)
            {
                MessageBox.Show("Please mention No of Copies correctly.",
                    Constant.AppTitle, MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                blnValidInput = false;
            }
            return blnValidInput;
        }
        catch (Exception ex)
        {
            CommonHelper.RaiseError(ex.Message, MODULE_NAME, "ValidInput");
            return false;
        }
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {
        try
        {
            if (ValidInput())
            {
                m_blnPrintAllPages = optAll.Checked;
                if (txtFrom.Text.Trim() != "")
                {
                    m_intFromPages = Convert.ToInt32(txtFrom.Text.Trim());
                }
                else
                {
                    m_intFromPages = 0;
                }
                if (txtTo.Text.Trim() != "")
                {
                    m_intToPages = Convert.ToInt32(txtTo.Text.Trim());
                }
                else
                {
                    m_intToPages = 0;
                }

                if (txtCoppies.Text.Trim() != "")
                {
                    m_intNoofCoppies = Convert.ToInt32(txtCoppies.Text.Trim());
                }
                else
                {
                    m_intNoofCoppies = 0;
                }
                //                    m_intToPages = Convert.ToInt32(txtTo.Text.Trim());
                //                    m_intNoofCoppies = Convert.ToInt32(txtCoppies.Text.Trim());
                m_blnCollated = chkCollated.Checked;
                m_strPrinterName = cmbPrint.Text;
                //m_strPrinterDriver = PrinterSettings.     ;
                //m_strPrinterPort = PrinterPort;

                m_blnSelected = true;
                this.Close();
            }
        }
        catch (Exception ex)
        {
            CommonHelper.RaiseError(ex.Message, MODULE_NAME, "btnPrint_Click");
        }
    }

    private void txtCoppies_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.KeyChar = (char)(clsKeyValidator.ValidateKey(e.KeyChar,
                        e.KeyChar.ToString(), txtCoppies.Text.Trim(),
                        txtCoppies.SelectionStart,
                        clsKeyValidator.enuTextType.ttNumber, 
                        clsKeyValidator.enuAlphabateCase.acMixedCase,
                        4, 0, false, false));
        CommonHelper.Send_Tab(e.KeyChar);
    }

    private void txtFrom_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.KeyChar = (char)(clsKeyValidator.ValidateKey(e.KeyChar,
                        e.KeyChar.ToString(), txtFrom.Text.Trim(),
                        txtFrom.SelectionStart,
                        clsKeyValidator.enuTextType.ttNumber,
                        clsKeyValidator.enuAlphabateCase.acMixedCase,
                        4, 0, false, false));
        CommonHelper.Send_Tab(e.KeyChar);
    }

    private void txtTo_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.KeyChar = (char)(clsKeyValidator.ValidateKey(e.KeyChar,
                        e.KeyChar.ToString(), txtTo.Text.Trim(),
                        txtTo.SelectionStart,
                        clsKeyValidator.enuTextType.ttNumber,
                        clsKeyValidator.enuAlphabateCase.acMixedCase,
                        4, 0, false, false));
        CommonHelper.Send_Tab(e.KeyChar);
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        m_blnSelected = false;
        this.Close();
    }

    private void cmbPrint_SelectedIndexChanged(object sender, EventArgs e)
    {
        m_strPrinterName = cmbPrint.Text ;
    }

    private void optPages_CheckedChanged(object sender, EventArgs e)
    {
        grbPages.Enabled = optPages.Checked;
    }

    private void txtCoppies_TextChanged(object sender, EventArgs e)
    {

    }
}
}
  

Комментарии:

1. Какой код вы используете для диалога? Какую версию библиотеки CR вы используете?

2. Я использую SAP Crystal Reports, версию для Microsoft Visual Studio 13.0.12.1494, и для кода я редактирую вопрос.