Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load in memory text into WebBrowser control

Tags:

.net

controls

On the .Net WebBrowser control the only way I can see to load a page to it is to set the URL property. But I would like to instead give it some HTML code that I already have in memory without writing it out to a file first. Is there any way to do this? Or are there any controls that will do this?

like image 473
Adam Haile Avatar asked Sep 29 '08 18:09

Adam Haile


People also ask

How to load and display HTML file in WebBrowser control?

In this article I will explain with an example, how to load and display HTML file in WebBrowser control in Windows Application (Windows Forms Application) using C# and VB.Net. In order to display HTML file in WebBrowser control in a Windows Application, the HTML file is added to the project as an Embedded Resource.

How to read the content of streamreader in WebBrowser control?

Finally the contents of the StreamReader are read using the ReadToEnd method and are assigned to the WebBrowser control. StreamReader reader = new StreamReader(assembly.GetManifestResourceStream ("WebBrowser_HTML_File_CS.HTML.htm"));

What's wrong with webbrowser1 documenttext?

The main issue being that after the first assignment of webBrowser1.DocumentText to some html, subsequent assignments had no effect.

Is the WebBrowser instance still valid?

The WebBrowser instance is no longer valid. A reference to the underlying native WebBrowser could not be retrieved. The following example shows how to navigate to a stream that contains a document. If the stream parameter is null, WebBrowser navigates to a blank document ("about:blank").


2 Answers

You want the DocumentText Property:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documenttext.aspx

?

from http://www.codeguru.com/forum/showpost.php?p=1691329&postcount=9 : Also you should provide a couple things:

  • Don't set DocumentText in the constructor. Use Form_Load or your own method. If you set DocumentText in the constructor, you will not be able to set it again anywhere in the application. Be sure to check that the Form Designer hasn't set it either.

  • You can only set DocumentText once per method call. This is odd and most likely a bug, but it's true. For example: setting DocumentText in a for-loop will only set properly on the first iteration of the loop. You can however, create a small method to set DocumentText to the passed in string, then call this method in a for-loop.

like image 183
mspmsp Avatar answered Oct 05 '22 13:10

mspmsp


You use either WebBrowser.DeocumentText (http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documenttext.aspx) or WebBrowser.DocumentStream (http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentstream.aspx) to change the HTML in the current document. You might need to navigate to about:blank, if you don't have a document.

like image 31
Franci Penov Avatar answered Oct 05 '22 12:10

Franci Penov