0

I am trying to display some html in a WebBrowser control in a windows form and would like to resize the form based on the html contents.

I can do this by looking at webBrowser1.Document.Body.ScrollRectangle.Size in the DocumentCompleted event. However that event is not triggered until the form is displayed. Hence I need to display the form and then re-size it.

Is there any way to avoid this flashing and get the form initialised to the correct size?

sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • In the code that opens this Form, just create the instance, e.g. `var f = new FormBrowser();` (assume this is the class name of your Form). In the `FormBrowser` Constructor , call `[WebBrowser].Navigate("[https://Some address]");`. In `DocumentCompleted`, after `var browser = sender as WebBrowser; if (browser.ReadyState != WebBrowserReadyState.Complete) return;`, measure the Document and call `this.Show();`. – Jimi Nov 30 '20 at 11:47
  • The code you find here: [WebBrowser Html Document to Image](https://stackoverflow.com/a/60741246/7444103) could help with the size of the HTML Document, in case it's needed. – Jimi Nov 30 '20 at 11:50
  • @Jimi Firstly, I am not navigating to a website, just trying to set the DocumentText to an existing html string. Secondly, the linked answer you point to is using the DocumentCompleted event, which does not fire until my form is shown. – sgmoore Nov 30 '20 at 12:10
  • Navigating to a HTTP address or setting the Document content manually (I assume you use `HtmlDocument.Write()`) is the same thing. The `DocumentCompleted` event has nothing to do with the Form being shown or not: that event is raised *somewhere else* and its delegate is called no matter what the state of the Form, once the Form has being initialized (after `InitializeComponent()` has completed) or the event has been otherwise subscribed to. – Jimi Nov 30 '20 at 12:26
  • I'm just setting `webBrowser1.DocumentText` . Also if I leave out the `f.ShowDialog();` command then the DocumentCompleted is never called. – sgmoore Nov 30 '20 at 12:59
  • You should use `[HtmlDocument].Write()`, but that's up to you. In the scenario I described (read again my first comment), `Form.Show()` is used. You cannot call `ShowDialog();` in this context, otherwise the Form is shown twice. You can pass the owner in a Constructor overload, then set it after `DocumentCompleted` is *completed*, as `this.Visible = false; this.Show([The Owner Instance passed in my Constuctor]);` – Jimi Nov 30 '20 at 13:13
  • Thanks for you help, but my real program is a plugin to a third-party app and I just pass the form to it, so I can't avoid them calling ShowDialog(). Anything I think I have found a solution. – sgmoore Nov 30 '20 at 15:03

1 Answers1

0

The solution/workaround seems to be rather simple.

After setting the html using

webBrowser1.DocumentText = html;

and before displaying the form, just call

System.Windows.Forms.Application.DoEvents();

This means the DocumentCompleted will be triggered before the form is shown.

sgmoore
  • 15,694
  • 5
  • 43
  • 67