Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need some help. My requirement is when the user click on print button it should show a print preview dialog. if user cancel or close the preview dialog the form must be return to original.

the problem i am facing is print preview dialog box is visible. but i don't know how to capture the print preview tools click events

C#
public void print()
    {
        PrintDialog pd = new PrintDialog();
        PrintDocument pdoc = new PrintDocument();
        PrinterSettings ps = new PrinterSettings();
        PaperSize psize = new PaperSize(); 
        pdoc.DefaultPageSettings.Landscape = false;
        pd.Document = pdoc;
        pd.Document.DefaultPageSettings.PaperSize = psize;

        pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);

         DialogResult result = pd.ShowDialog();
         if (result == DialogResult.OK)
         {

             PrintPreviewDialog ppd = new PrintPreviewDialog();
             ppd.Document = pdoc;
             ppd.ShowDialog();

             pdoc.Print();
         }
}

if i place the PrintPreviewDialog code before pd.ShowDialog() nothing visible in the preview mode. this not working
C#
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pdoc;
ppd.ShowDialog();
 DialogResult result = pd.ShowDialog();
 if (result == DialogResult.OK)
 {
     pdoc.Print();
 }
Posted
Updated 12-Mar-21 23:52pm

Hello ,
Try this
private void printButton_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);

    PrintDialog printdlg = new PrintDialog();
    PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();
    
    printPrvDlg.Document = pd;
    printPrvDlg.ShowDialog(); 

    printdlg.Document = pd;
    if (printdlg.ShowDialog() == DialogResult.OK)
    {
        pd.Print();
    }
}

thanks
 
Share this answer
 
You're catching dialog result from pd (PrintDialog) not ppd.

You should have additional DialogResult like this:

public void print()
    {
        PrintDialog pd = new PrintDialog();
        PrintDocument pdoc = new PrintDocument();
        PrinterSettings ps = new PrinterSettings();
        PaperSize psize = new PaperSize(); 
        pdoc.DefaultPageSettings.Landscape = false;
        pd.Document = pdoc;
        pd.Document.DefaultPageSettings.PaperSize = psize;
 
        pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
 
         DialogResult result = pd.ShowDialog();
         if (result == DialogResult.OK)
         {

             PrintPreviewDialog ppd = new PrintPreviewDialog();
             ppd.Document = pdoc;
/*This dialog result is the important one :)*/
             DialogResult ppdResult = ppd.ShowDialog();

            if (ppdResult == DialogResult.OK )
                 pdoc.Print();
            else
                pd.Close();
         }
}


If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
v3
Comments
jinesh sam 14-Oct-14 3:11am    
i am getting an error
System.Windows.Forms.PrintDialog' does not contain a definition for 'close' and no extension method 'close' accepting a first argument of type 'System.Windows.Forms.PrintDialog' could be found (are you missing a using directive or an assembly reference?
jinesh sam 14-Oct-14 3:16am    
i remove the else block then its working as i expected
Thanks...
Hello,

As per my understanding the requirement is to cancel print when user closes or cancel the preview dialog form.

So here assuming the print option will be chosen from the preview dialog form ,
My suggestion is to remove the pdoc.Print() call .

if (result == DialogResult.OK)
{

PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pdoc;
ppd.ShowDialog();

// pdoc.Print(); /* remove this call as user will print from preview form or cancel out*/
}

I created as demo code and it works .

Note : I added a blank pdoc_PrintPage() handler as below.

C#
private void pdoc_PrintPage(object sender, PrintPageEventArgs e)
       {

          // throw new NotImplementedException();

       }




Thanks
 
Share this answer
 
Comments
jinesh sam 14-Oct-14 3:27am    
your assumption was correct. since i made a call pdoc.Print() after ppd.ShowDialog() so when the user cancel the preview its showing the print dialog again. As per your suggestion i remove the pdoc.Print() then its works fine. now the cancel button and close button performing the default action without any code. so thaks a lot
A Preview.Close() command after Preview.ShowDialog() can never be reached,
because you have to close the Preview in order to reach it. So you must
call Preview.Close() from somewhere else.

When printing starts with document.Print, document_StartPrint is called,
when printing is finished, document_EndPrint is called. It seems that
Preview.ShowDialog does internally call document.Print, but the output
is redirected to the preview window. When you click the printer icon in
Previwe, document.Print is called again, this time with the selected
printer.

If you put the Preview.Close() command in the EndPrint function, it is
called from outside the Preview and closes. But you must not close while
the preview is being produced, so EndPrint must know when this is the
case.

The solution which works for me is:

Declare the following global variables:

private static bool InPreview;
private static PrintPreviewDialog Preview;

Add the EndPrint function to your document, and gefore calling the dialog,
set InPreview to true:

PrintDialog PD = new PrintDialog();
...
doc = new PrintDocument();
doc.EndPrint += new PrintEventHandler(doc_EndPrint);
...
if (PD.ShowDiadog() == DialogResult.OK) {
...
InPreview = true;
Preview.ShowDialog();
}

The EndPrint function looks like this:

private static void doc_EndPrint(object sender, PrintEventArgs e) {
if(InPreview) {
InPreview = false;
} else {
if(Preview != null) {
Preview.Close();
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900