Search code examples
c#printingprintdialog

If Printer Dialog Cancelled, Then Don't Print?


If I run this code, and press cancel on the PrintDialog, it still prints. How can I tell if the use pressed cancel?

PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();

dialog.ShowDialog();
document.PrinterSettings = p.PrinterSettings;
document.Print();

Addendum

WebBrowser w = new WebBrowser();
w.ShowPrintDialog(); //.ShowPrintDialog returns a void, how can I deal with this?

Solution

  • You can check the result of the ShowDialog method:

    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
       //Print
    }