按下打印按钮时显示自定义打印对话框

时间:2016-10-24 13:57:50

标签: c# crystal-reports

我在C#windows应用程序中创建了自己的打印对话框。我想在用户单击Crystal Report查看器的打印按钮时显示此对话框。我在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) && 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 && ((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)
    {

    }
}
}

0 个答案:

没有答案