[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.
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).
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.
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)
Re: Writing text/HTML to WebBrowser control
Quote:
Originally Posted by
dunfiddlin
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. :)