CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    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?

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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!
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    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.
    Best regards,
    Igor Sukhov

    www.sukhov.net

  4. #4
    Join Date
    Feb 2007
    Posts
    11

    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() ?
    Zytan

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Set WebBrowser.DocumentText doesn't do anything

    Simply, you just navigate to .......... about:blank
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Feb 2007
    Posts
    11

    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!
    Zytan

  7. #7
    Join Date
    Jan 2008
    Posts
    2

    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!

  8. #8
    Join Date
    Jan 2008
    Posts
    2

    Re: Set WebBrowser.DocumentText doesn't do anything

    Try using Document.OpenNew before calling Document.Write.

    I would still have expected an exception.

  9. #9
    Join Date
    Aug 2005
    Location
    Seattle, Wa
    Posts
    179

    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

  10. #10
    Join Date
    Apr 2011
    Posts
    1

    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)

  11. #11
    Join Date
    May 2011
    Posts
    1

    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();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured