C
c j anderson, mcp
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(strhtmlbrowserlc) +
strhtmlbrowser.Length, "</span>")
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowserlc),
"<span style=""background-color: yellow"">")
txtHTML.Text = strhtmlui
End If
End If
End Sub
Cheers.
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(strhtmlbrowserlc) +
strhtmlbrowser.Length, "</span>")
strhtmlui = strhtmlui.Insert(strhtmluilc.IndexOf(strhtmlbrowserlc),
"<span style=""background-color: yellow"">")
txtHTML.Text = strhtmlui
End If
End If
End Sub
Cheers.