Set WebBrowser.DocumentText doesn't do anything
Hello!
I just started with .net. I made a new C++-CLR-Windows-Forms-Applicationin VS 2005, which has one simple form by default. I added a WebBrowser-Control to it.
Now I just want to set the html-code inside the WebBrowser-Control. I found the DocumentText-Property and set it:
Code:
Form1(void)
{
InitializeComponent();
//
this->webBrowser1->DocumentText = L"<html><body>It works!</body></html>";
//
}
Ok, the program runs, but it shows nothing inside webBrowser1. I had a look at the sourcecode via the popup-menu of the WebBrowser, it just says "<HTML></HTML>".
What am I doing wrong?
Re: Set WebBrowser.DocumentText doesn't do anything
it's not for html, and youll probably find the browser is reluctant to do anyhting before you navigate()
its a pain in the abackside, but i've actually found the easiest way to get a web browser to show something is to write that something to disk and then Navigate2() the file you wrote.. it sounds lame, but it'll save you a lot of hair pulling!
Re: Set WebBrowser.DocumentText doesn't do anything
Quote:
Originally Posted by cjard
it's not for html, and youll probably find the browser is reluctant to do anyhting before you navigate()
its a pain in the abackside, but i've actually found the easiest way to get a web browser to show something is to write that something to disk and then Navigate2() the file you wrote.. it sounds lame, but it'll save you a lot of hair pulling!
Much more elegant and effective way of populating web browser control with html is to use Write method of Document object of WebBrowser control.
Re: Set WebBrowser.DocumentText doesn't do anything
Quote:
Originally Posted by Igor Soukhov
Much more elegant and effective way of populating web browser control with html is to use Write method of Document object of WebBrowser control.
When the WebBrowser control is first made, the Document object is null. So, how do you get to the point where you can invoke Document.Write() ?
Re: Set WebBrowser.DocumentText doesn't do anything
Simply, you just navigate to .......... about:blank :D :D :D
Re: Set WebBrowser.DocumentText doesn't do anything
Ok, I was thinking that. So, that implies it has not navigated ANYWHERE, not even about:blank, before you tell it to do so.
Thanks! :)
Re: Set WebBrowser.DocumentText doesn't do anything
I try this setting DocumentText and Document.Write and it works fine on the first try but when I try to change the display to another page, it doesn't work. Whatever I displayed first remains forever.
If I navigate to blank then it will go blank but Document.Write basically no longer works.
I tried Invalidate too.
It's really funny to me how I can set a property and then read it and it contains the value it had before I set it. If it's a readonly property then don't let me set it! Throw an exception or give me a compile error!
Re: Set WebBrowser.DocumentText doesn't do anything
Try using Document.OpenNew before calling Document.Write.
I would still have expected an exception.
Re: Set WebBrowser.DocumentText doesn't do anything
What the OP is doing with his code will work fine provided 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.
-C6
Re: Set WebBrowser.DocumentText doesn't do anything
I like to use the WB to play youtube vids, because Flash drives me crazy. The WB just needs a little push, like application.doevents. I haven't needed to naviate to about:blank or anything else to make this work.
Dim DocText as string= "Your Text"
WebBrowser1.DocumentText = DocText
Application.DoEvents
(Without DoEvents, it won't que the next video, and either it goes blank, or plays the old video again)
Re: Set WebBrowser.DocumentText doesn't do anything
This works fine for me:
string html = textBox1.Text;
html = html.Replace("\r\n", "<br />");
webBrowser.Navigate("about:blank");
webBrowser.Document.OpenNew(false);
webBrowser.Document.Write(html);
webBrowser.Refresh();