开发者

Show Dialog with WebBrowser

开发者 https://www.devze.com 2023-02-11 05:03 出处:网络
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开发者_高

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?


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


  • 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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消