1

I want to show a printer dialog but when I run the following code, it prints right away while the ShowPrinterDialog is open. In other words, it doesn't wait for the ShowPrinterDialog to close before printing.

WebBrowser browser = new WebBrowser();
browser.DocumentText = "<b>Stack Overflow FTW!</b>";
browser.DocumentCompleted += Browser_DocumentCompleted;

void Browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ((WebBrowser)sender).ShowPrinterDialog();
    ((WebBrowser)sender).Print();
}

Any ideas on how to fix this?

2 Answers 2

5

Just remove the last line with .Print() ;-)

0
  • If you don't use WebBrowserDocumentCompletedEventArgs, declare it just as EventArgs
  • Don't cast more then once

i.e.:

void Browser_DocumentCompleted(object sender, EventArgs e)
{
     WebBrowser browser = (WebBrowser)sender;
     browser.ShowPrinterDialog();
     browser.Print();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.