Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add print dialog to the printpreviewdialog?

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();
like image 512
yong.k Avatar asked Oct 25 '16 09:10

yong.k


1 Answers

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());
    }
}
like image 171
Ufuk Onder Avatar answered Oct 10 '22 13:10

Ufuk Onder