How to add print dialog to the printpreviewdialog?

只愿长相守 提交于 2020-01-01 05:42:09

问题


My boss wants me to create the window form that has printing function, but he wants to print the datagridview after preview.

So now I encourage the problem, I cannot print multiple set of paper or choose the printer or make any changes when click the print button on printpreviewdialog.When I click the button, it will direct print the paper. So I wish to join the printpreviewdialog and printdialog.

Why the printpreviewdialog and printdialog can only be used in different buttons? It's lack of usibility when needed to click one button to preview and click another button to print multiple set and make changes of printer.

Any one can help me?

printdialog

DialogResult result = printDialog1.ShowDialog();
            // If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                position = 0;
                pageno = 1;
                printDocument2.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
                printDocument2.OriginAtMargins = true;
                printPreviewDialog1.Document = printDocument2;
                printPreviewDialog1.ShowDialog();
            }   

printpreviewdialog

printDocument3.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
            printDocument3.OriginAtMargins = true;
            //((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled = false;
            printPreviewDialog1.Document = printDocument3;
            printPreviewDialog1.ShowDialog();

回答1:


I know it is late, but i think someone will still need that. As Hans Passant say, "print preview is heavily depended on printer and page settings." But there is a print-button in printpreviewdialog, which is still reasonable for most cases. But that button directly prints to your default printer, and never shows a dialog. If you want a print dialog from printpreview dialog, you can just manipulate ToolStrip of PrintPreviewDialog.

Here it goes (assuming you initialized printPreviewDialog1, printDialog1 and printDocument1 objects)

printPreviewDialog1.Document = printDocument1;
ToolStripButton b = new ToolStripButton();
b.Image = Properties.Resources.PrintIcon;
b.DisplayStyle = ToolStripItemDisplayStyle.Image;
b.Click += printPreview_PrintClick;
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
printPreviewDialog1.ShowDialog();

Using above code, you can remove default print button on ToolStrip of PrintPreview and replace it with a newly created "print button". This button now has an Click event handler, and by using it, you can show the PrintDialog.

private void printPreview_PrintClick(object sender, EventArgs e)
{
    try
    {
        printDialog1.Document = printDocument1;
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.Print();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, ToString());
    }
}



回答2:


Works good... Have one tip... You can re-use the current icon by:

this.ToolStripButton.Image = ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).ImageList.Images[0];

The rest of the snippet:

        {
        this.ToolStripButton = new System.Windows.Forms.ToolStripButton();
        this.ToolStripButton.Image = ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).ImageList.Images[0];
        this.ToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
        this.ToolStripButton.Click += new System.EventHandler(this.printPreview_PrintClick);
        ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).Items.RemoveAt(0);
        ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).Items.Insert(0, ToolStripButton);
    }
    private void printPreview_PrintClick(object sender, System.EventArgs ee)
    {
        try
        {
            this.printDialog.Document = printDocument;
            if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                printDocument.Print();
            }
        }
        catch (System.Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message, ToString());
        }
    }
    private System.Windows.Forms.ToolStripButton ToolStripButton;



回答3:


The snippet provided by @AceIndy above, does not take into account if the user changes the default printer or its settings. This is how I solved that problem:

private void printPreview_PrintClick(object sender, EventArgs e)
{
    try
    {
        printDialog.Document = printDocument;

        if (printDialog.ShowDialog() == DialogResult.OK)
        {
            printDocument.PrinterSettings = printDialog.PrinterSettings;

            printDocument.Print();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, ToString());
    }
}


来源:https://stackoverflow.com/questions/40236241/how-to-add-print-dialog-to-the-printpreviewdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!