By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,579 Members | 1,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,579 IT Pros & Developers. It's quick & easy.

Persisting Changes Made in WebBrowser back to source file

P: n/a
I recently had a need to be able to highlight selected text in a web
browser control (working code is posted below). Attempts to access the
modified html source through the webbrowser or the mshtml COM interface
(see below), however, have failed and I resorted to inserting the
changes into the file by the means shown... This method is not 100%
successful because the innerHTML reported by the COM object is
*different* from what is actually in the source (spaces are
added/removed; elements & attributes are sometime capitalized; etc.) It
seems like there should be a reliable way to do this via either the
WebBrowser Windows Form Control or the MSHTML component -- does anyone
know of a better way?

Private Sub HighlightToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
HighlightToolStripMenuItem.Click
'we have to resort to COM to play with the underlying HTML in the
browser

'get the IHTMLDocument2 object
Dim htmldoc2 As mshtml.IHTMLDocument2
htmldoc2 = CType(WebBrowser1.Document.DomDocument,
mshtml.IHTMLDocument2)

'get the IHTMLTextRange object
Dim textrange As mshtml.IHTMLTxtRange =
CType(htmldoc2.selection.createRange, mshtml.IHTMLTxtRange)

If (Not String.IsNullOrEmpty(textrange.htmlText)) Then

Dim strhtmlbrowser As String = textrange.htmlText
Dim strhtmlbrowserlc As String = strhtmlbrowser.ToLowerInvariant()

'keywords file needs to be loaded to get list of allowed colors
Dim frm As New frmColors(arstrAllowedColors)
frm.ShowDialog()

textrange.pasteHTML("<span style=""background-color: " &
frm.SelectedColor & """>" & strhtmlbrowser & "</span>")
frm.Close()

textrange = CType(htmldoc2.selection.createRange,
mshtml.IHTMLTxtRange)

Dim strhtmlui As String = txtHTML.Text
Dim strhtmluilc As String = strhtmlui.ToLowerInvariant()
If (strhtmluilc.IndexOf(strhtmlbrowserlc) <> -1) Then
'work from back to keep index valid
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowse rlc) +
strhtmlbrowser.Length, "</span>")
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowse rlc),
"<span style=""background-color: yellow"">")

txtHTML.Text = strhtmlui

End If
End If

End Sub

Cheers.
---
http://code.box.sk
nemo me impune lacessit

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I still haven't found a better solution... any ideas on how to make
changes in the browser show in the source view? the modified HTML
source code must exist somewhere...

Cheers.
---
http://code.box.sk
nemo me impune lacessit

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #2

P: n/a
It wasn't completely clear to me what your actual requirement and/or
subsequent problem was from your post.

It sounds like you want to highlight some selected text (presumably in a
manner other than normal highlighting), and that you resorted to a technique
that itself has problems. Is that correct?

If I have summarised correctly then I would not ordinarily highlight text by
modifying the underlying html. Instead I would use one of two methods.

To highlight an element, I would use a runtime style. This available on the
IHTMLElement2 interface. Use the runtimeStyle property to set
backgroundColor and color.

To highlight individual bits of text, I would use rendering services,
available on the IHTMLRenderingServices interface. This technique is a lot
more complicated, but may be more what you are looking for. Essentially, you
create markup pointers to define the start and end of your selection, and
place a display pointer at each location. You then create a render style in
which you define the background and foreground colours, and then call
AddSegment on the IHTMLRenderingServices interface. MSDN has a sketchy
outline of how to do this. Feel free to post back if this is the way you
want to go.

HTH

Charles
<c j anderson>; "mcp" <st*********@hotmail.com> wrote in message
news:OI**************@TK2MSFTNGP14.phx.gbl...
I recently had a need to be able to highlight selected text in a web
browser control (working code is posted below). Attempts to access the
modified html source through the webbrowser or the mshtml COM interface
(see below), however, have failed and I resorted to inserting the
changes into the file by the means shown... This method is not 100%
successful because the innerHTML reported by the COM object is
*different* from what is actually in the source (spaces are
added/removed; elements & attributes are sometime capitalized; etc.) It
seems like there should be a reliable way to do this via either the
WebBrowser Windows Form Control or the MSHTML component -- does anyone
know of a better way?

Private Sub HighlightToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
HighlightToolStripMenuItem.Click
'we have to resort to COM to play with the underlying HTML in the
browser

'get the IHTMLDocument2 object
Dim htmldoc2 As mshtml.IHTMLDocument2
htmldoc2 = CType(WebBrowser1.Document.DomDocument,
mshtml.IHTMLDocument2)

'get the IHTMLTextRange object
Dim textrange As mshtml.IHTMLTxtRange =
CType(htmldoc2.selection.createRange, mshtml.IHTMLTxtRange)

If (Not String.IsNullOrEmpty(textrange.htmlText)) Then

Dim strhtmlbrowser As String = textrange.htmlText
Dim strhtmlbrowserlc As String = strhtmlbrowser.ToLowerInvariant()

'keywords file needs to be loaded to get list of allowed colors
Dim frm As New frmColors(arstrAllowedColors)
frm.ShowDialog()

textrange.pasteHTML("<span style=""background-color: " &
frm.SelectedColor & """>" & strhtmlbrowser & "</span>")
frm.Close()

textrange = CType(htmldoc2.selection.createRange,
mshtml.IHTMLTxtRange)

Dim strhtmlui As String = txtHTML.Text
Dim strhtmluilc As String = strhtmlui.ToLowerInvariant()
If (strhtmluilc.IndexOf(strhtmlbrowserlc) <> -1) Then
'work from back to keep index valid
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowse rlc) +
strhtmlbrowser.Length, "</span>")
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowse rlc),
"<span style=""background-color: yellow"">")

txtHTML.Text = strhtmlui

End If
End If

End Sub

Cheers.
---
http://code.box.sk
nemo me impune lacessit

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #3

P: n/a
thanks for the reply.

my need is to change what is displayed in the browser and then save it
back to file. the problem is not with the pasteHtml (per se), but
rather with being able to see the changes... if I select "View Source"
from the context menu, I only see the original document, not the
changes...

having looked at the source that comes from the serviced component, I
doubt that I will be able to use it (it's changing the underlying source
in ways that makes it invalid XHTML Strict)

I will look into the methods that you suggested though to see if they
simplify the process from what I am currently employing.

Cheers.
---
http://code.box.sk
nemo me impune lacessit

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.