C# WebBrowser.ShowPrintDialog() not showing

Posted by jeah_wicer on Stack Overflow See other posts from Stack Overflow or by jeah_wicer
Published on 2012-12-11T19:43:34Z Indexed on 2012/12/11 23:03 UTC
Read the original article Hit count: 1733

Filed under:
|
|

I have this peculiar problem while wanting to print a html-report. The file itself is a normal local html file, located on my hard drive.

To do this, I have tried the following:

    public static void PrintReport(string path)
    {
        WebBrowser wb = new WebBrowser();
        wb.Navigate(path);
        wb.ShowPrintDialog()
    }

And I have this form with a button with the click event:

    private void button1_Click(object sender, EventArgs e)
    {
        string path = @"D:\MyReport.html";
        PrintReport(path);
    }

This does absolutely nothing. Which is kind of strange... but things get stranger...

When editing the print function to do the following:

    public static void PrintReport(string path)
    {
        WebBrowser wb = new WebBrowser();
        wb.Navigate(path);
        MessageBox.Show("TEST");
        wb.ShowPrintDialog()
    }

It works. Yes, only adding a MessageBox. The MessageBox is showing and after it comes the print dialog. I have also tried with Thread.Sleep(1000) instead, which doesn't work. Can anyone explain to me what's going on here? Why would a messagebox make any difference?

Can it be some kind of permission problem? I've reproduced this on both Windows 7 and 8, same thing. I made this small application with only the above code to isolate the problem. I am quite sure it works on windows XP though, since an older version of the application I'm working on runs on it. When trying to do this directly with the mshtml-dll instead I also get problems.

Any input or clarification is greatly appreciated!

© Stack Overflow or respective owner

Related posts about c#

Related posts about html