0

I'm using a WebBrowser control in my Visual C# (.net 2.0) application.
Now I'd like to add a print button which shows the page setup dialog and then directly prints when the user presses the OK button or cancels the printing when the user presses the cancel button.
However WebBrowser.ShowPageSetupDialog doesn't return DialogResult, but just void.
Is there something I've missed or any other way to know the users action?

Marc
  • 9,012
  • 13
  • 57
  • 72

2 Answers2

1

The page setup dialog box off a WebBrowser control sets registry entries. What I've done in the past was to set those values for the user in code and only gave them the option to print.

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
1

I had the same problem, was able to find a gimmicky workaround by observing the way IE Page Setup stores margin values in the @"Software\Microsoft\Internet Explorer\PageSetup" registry.

When you press the OK button in IE Page Setup, it stores the margin values written in the setup as string (REG_SZ) with a length of 8, with the remaining space padded with 0s.

i.e.

0.75 is stored as 0.750000

1.0 is stored as 1.000000

2 is stored as 2.000000

When you use WebBrowser.Print(), it converts the margin values into floating points, so having 0.75 or 0.750000 as margin values in the registry produces the same result.

However, if you compare them as strings, 0.75 and 0.750000 would be regarded as different.

And here comes the trick :

  1. Before calling WebBrowser.ShowPageSetupDialog(), remove the trailing 0s in registry's margin values

i.e.

0.750000 -> 0.75

0.500000 -> 0.5

1.000000 -> 1

  1. Store one of the margin values inside a string variable

  2. Call WebBrowser.ShowPageSetupDialog()

  3. If the user pressed OK, the margin values in the registry would be padded back with 0s. Else, they would remain trimmed as mentioned in point 1.

  4. Compare the margin values in the registry with the one stored in the variable, if they are the same, then the user pressed 'Cancel', otherwise the user pressed 'OK'.

Example :

private void ie_DocumentCompleted(object _sender, WebBrowserDocumentCompletedEventArgs e)
{
    System.Windows.Forms.WebBrowser ie = (System.Windows.Forms.WebBrowser)_sender;

    string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
    bool bolWritable = true;
    RegistryKey ok = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);

    ok.SetValue("margin_left", 0.75, RegistryValueKind.String);

    string reg_validation = (string) ok.GetValue("margin_left");

    ie.ShowPageSetupDialog();

    if (reg_validation.Equals((string)ok.GetValue("margin_left")))
    {
        MessageBox.Show("Cancel");
    }
    else
    {
        MessageBox.Show("OK");
        ie.Print();
    }
    ok.Close()
}
Miscavel
  • 11
  • 3