如何关闭“打印预览”对话框?

时间:2018-11-27 14:01:52

标签: c# .net

我具有文档打印功能。我试图在用户按下“打印”按钮后关闭“打印预览对话框”表单。按下“打印预览”对话框中的“打印”按钮后,该事件将启动下面的功能以打印文档。我希望在我致电printPreviewDialog1.Close()时关闭该表单,但它只是越过线路而没有任何反应。

但是在完成打印作业后,它不会关闭打印预览对话框窗体。

    public void _start_Print(object sender, EventArgs e)
    {
        printDocument1.Print();

    }

根据注释中的要求添加。初始化打印预览对话框

    private void btnPrint_Click(object sender, EventArgs e)
    {
        PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
        printPreviewDialog1.Document = printDocument1;

        ToolStrip ts = new ToolStrip();
        ts.Name = "wrongToolStrip";
        foreach (Control ctl in printPreviewDialog1.Controls)
        {
            if (ctl.Name.Equals("toolStrip1"))
            {
                ts = ctl as ToolStrip;
                break;
            }
        }

        ToolStripButton printButton = new ToolStripButton();
        ToolStripButton closeButton = new ToolStripButton();

        foreach (ToolStripItem tsi in ts.Items)
        {
            if (tsi.Name.Equals("printToolStripButton"))
            {
                printButton = tsi as ToolStripButton;
            }
            else if (tsi.Name.Equals("closeToolStripButton"))  // idk if this is the name of the close button im trying to programmatically close it after printing
            {
                closeButton = tsi as ToolStripButton;
            }
        }

        ts.Items.Remove(printButton);
        ToolStripButton b = new ToolStripButton();
        b.ImageIndex = printButton.ImageIndex;
        b.Visible = true;
        ts.Items.Insert(0, b);
        b.Click += new EventHandler(this._start_Print);

        printPreviewDialog1.WindowState = FormWindowState.Maximized;
        printPreviewDialog1.ShowDialog();
        printPreviewDialog1.Dispose(); //< doesnt do anything 
        closeButton.PerformClick(); // < doesn't do anything, possibly using wrong name for toolstripbutton

    }

根据注释中的要求初始化打印文档

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawRectange(Pens.Black, 60, 60, 60,60);
    }

1 个答案:

答案 0 :(得分:0)

已解决。

我创建了一个全局ToolStripButton,并在closeToolStripButton中用printPreviewDialog初始化了此按钮。

然后,在完成打印作业后,我以编程方式单击了此按钮。

    private ToolStripButton closeButton = new ToolStripButton();  <-- global variable

    private void btnPrint_Click(object sender, EventArgs e)
    {
        foreach (ToolStripItem tsi in ts.Items)
        { 
            if (tsi.Name.Equals("closeToolStripButton"))
            {
                closeButton = tsi as ToolStripButton;
            }
            else if (tsi.Name.Equals("printToolStripButton"))
            {
                printButton = tsi as ToolStripButton;
            }
        }

        ts.Items.Remove(printButton);
        ToolStripButton b = new ToolStripButton();
        b.ImageIndex = printButton.ImageIndex;
        b.Visible = true;
        ts.Items.Insert(0, b);
        b.Click += new EventHandler(this._start_Printer); //<-- this starts the printer event where i "PerformClick() on the initialized closeButton"

        printprevDialog.WindowState = FormWindowState.Maximized;
        printprevDialog.ShowDialog();

    }


    public void _start_Printer(object sender, EventArgs e) // <--- then i just performed the close click here right after i hit the print button
    {
        printDocument1.Print();
        closeButton.PerformClick(); // <-- this way im not violating cross thread operations
    }