Chris
Chris

Reputation: 27394

Embedded WebBrowser in Windows Form C# project

I have a form with an embedded web browser control on it. I am currently using WebBrowser and use it like so:

webBrowser1.Navigate("about:blank");
HtmlDocument doc = this.webBrowser1.Document;
doc.Write(string.Empty);
String htmlContent = GetHTML();
doc.Write(htmlContent);

This writes the HTML correctly to the web browser control BUT it never clears the existing data and it just appends, so I end up with N web pages stacked on top of each other.

Is this the best control to use? If so why is it not clearing existing data?

Upvotes: 3

Views: 5647

Answers (3)

stuartd
stuartd

Reputation: 73303

Call HtmlDocument.OpenNew between pages:

OpenNew will clear the previous loaded document, including any associated state, such as variables. It will not cause navigation events in WebBrowser to be raised.

Upvotes: 0

Teoman Soygul
Teoman Soygul

Reputation: 25742

You need to use:

HtmlDocument doc = this.webBrowser1.Document.OpenNew(true);

now the contents of the document will be cleared before writing.

All calls to Write should be preceded by a call to OpenNew, which will clear the current document and all of its variables. Your calls to Write will create a new HTML document in its place. To change only a specific portion of the document, obtain the appropriate HtmlElement and set its InnerHtml property.

Upvotes: 3

Meligy
Meligy

Reputation: 36624

Yes, it is.

You should be able to call the Clear method if you need to clear contents.

Check this article for in-depth details and sample code:
http://www.codeproject.com/KB/miscctrl/simplebrowserformfc.aspx

Upvotes: 0

Related Questions