Results 1 to 4 of 4

Thread: DocumentText property of WebBrowser control not change

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    32

    Talking DocumentText property of WebBrowser control not change

    Hi, every experts!

    I have an issue that my program's WebBrowser Control doesn't accept DocumentText property change
    to what I set to.
    Actually, WebBrowser control which I put on main UI from from design time can accept all changes.
    I can set like "<html>....</html>" and so on, I can use any of activeX object in this WebBrowser control.
    However, if I create WebBrowser control at running time by thread, I can change any properties of this
    created control but DocumentText property.

    Basically DocumentText property have empty string "" when it first created, but when I assign any kind of
    DocumentText string, it changes always like "<html></html>", so it always shows empty space.
    I don't know why my VB.net act like this, but there is exist some kind of solution which can solve this.

    This is code sample I use.

    Code:
    Private MainThread as Thread
    
    Private Sub Main
    
        MainThread = New Thread(AddressOf WebDisplay)
        MainThread.SetApartmentState(ApartmentState.STA)
        MainThread.Start()
    
    End Sub
    
    Private Sub WebDisplay
    
       Dim MainBox as PictureBox
       Dim MainWeb as WebBrowser
    
       MainBox = New PictureBox
       MainWeb = New WebBrowser
       
       With MainBox
          .BorderStyle = BorderStyle.FixedSingle
          .Controls.Add(MainWeb)
          .Width = 100
          .Height = 100
       End With
    
       With MainWeb
           .Dock = DockStyle.Fill
           .ScrollBarsEnabled = False
           .IsWebBrowserContextMenuEnabled = False
           .WebBrowserShortcutsEnabled = False
       End With
    
       Console.Writeln(MainWeb.DocumentText)  ' Show value when it is first created
       MainWeb.DocumentText = "<html><script language='javascript'>.....</script><body>...</body></html>"
       Console.Writeln(MainWeb.DocumentText)  ' After DocumentText change
    
       .
       . Codes for add controls to main UI and show
       .
    
     End Sub
    There is two lines of Console.Writeln, so I can see DocumentText value change status.
    Even I made stop point on that line and look inside, it also same as I explained.
    First line can see "" (empty) string value of DocumentText property.
    After I set DocumentText property and see this value, it always show "<html></html>" only.

    Do you have any idea about this weird behavior?
    Thanks for advance of your help!!!

  2. #2
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: DocumentText property of WebBrowser control not change

    I think I understand your problem. The issue is that there is no Document created on which to set the text. If you check the browser's Document property, it will be null (Nothing).
    What I have done is to navigate to "About:Blank". i.e.
    Code:
    wb.Navigate("About:Blank")
    This creates a Document.

    There may be a better way of doing this, but I never found it.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2014
    Posts
    32

    Re: DocumentText property of WebBrowser control not change

    Quote Originally Posted by TnTinMN View Post
    I think I understand your problem. The issue is that there is no Document created on which to set the text. If you check the browser's Document property, it will be null (Nothing).
    What I have done is to navigate to "About:Blank". i.e.
    Code:
    wb.Navigate("About:Blank")
    This creates a Document.

    There may be a better way of doing this, but I never found it.
    Thanks for your reply TnTinMN,
    However it doesn't change situation.
    The thing is, WebBrowser control which created at design time on main form, it doesn't need
    to set url or anything. Just put content string to DocumentText property, it works nicely.
    But the control which made programatically, or maybe in thread, it doesn't do any.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,954

    Re: DocumentText property of WebBrowser control not change

    step 1... STOP using a secondary thread. To be honest I'm surprised you're not getting a cross thread error. UI interaction only happens on the main thread, and should be kept there. This could be part of the problem, I doubt it, but you never know. Also, there isn't any reason to use a secondary thread in this case.
    Also, you add the webbrowser to a picture box (why?) but you never add the picture box to the form.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

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