Results 1 to 5 of 5

Thread: [RESOLVED] Writing text/HTML to WebBrowser control

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Resolved [RESOLVED] Writing text/HTML to WebBrowser control

    I am working on a basic web page editor using mshtml.dll and a WebBrowser control. I want to switch between "design view" and "code view" - the latter showing the HTML tags etc. I can copy the HTML to a textbox to produce the code view and it works fine. Copying the HTML back to the WebBrowser control (allowing the user to make edits to the HTML tags and then show those changes in "design view") is not working properly. After a few clicks between design view and code view, the contents of the WebBrowser control start being duplicated. One line of text becomes two, then four, then eight etc.

    Here is some of my code.

    Initialisation:

    Code:
    Imports mshtml
    
        Dim Mdoc As IHTMLDocument2
    
            wb1.DocumentText = "<html><body></body></html>"
            Mdoc = wb1.Document.DomDocument
            Mdoc.designMode = "On"
    Design view - code view switching:

    Code:
                If optEditor.Checked Then
                    wb1.Visible = True
                    txtHTML.Visible = False
                    wb1.Document.Write(txtHTML.Text)
                Else
                    wb1.Visible = False
                    txtHTML.Visible = True
                    txtHTML.Text = wb1.DocumentText
                End If
    I guess I need to clear something before writing txtHTML.Text into the WebBrowser control, but I can't seem to find what it is.

    Any help would be much appreciated.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Writing text/HTML to WebBrowser control

    wb1.Document.Write(txtHTML.Text)

    This is append by default so set .DocumentText = "" immediately before it to avoid repetition (assuming you will be writing the whole document on each occasion).
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Writing text/HTML to WebBrowser control

    Doesn't work I'm afraid. The WebBrowser control goes blank and is not editable any more (no cursor appears when you click it). It's as if the tags get screwed up. I tried adding wb1.DocumentText = "<html><body></body></html>" after setting it to nothing but it still doesn't work.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Writing text/HTML to WebBrowser control

    No, you're right it doesn't. This, however, does (well for me anyway)

    wb1.Document.OpenNew(False)
    wb1.Document.Write(txtHTML.Text)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Writing text/HTML to WebBrowser control

    Quote Originally Posted by dunfiddlin View Post
    No, you're right it doesn't. This, however, does (well for me anyway)

    wb1.Document.OpenNew(False)
    wb1.Document.Write(txtHTML.Text)
    That is just perfect! Many thanks.

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