473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Persisting Changes Made in WebBrowser back to source file

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
3 1974
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Citoyen du Monde | last post by:
Trying to get some ideas on a simple javascript project (to teach myself the language). I want to develop a client-side vocabulary practice application that would allow users to enter their own...
9
by: Tim D | last post by:
Hi, I originally posted this as a reply to a rather old thread in dotnet.framework.general and didn't get any response. I thought it might be more relevant here; anyone got any ideas? My...
2
by: Lee | last post by:
Hi, I am using the WebBrowser control included with VS2005 to view XML files, and they are being displayed correctly as far as I can tell. The problem I am having is saving these XML files to...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
5
by: mabond | last post by:
Hi recently read a posting and reply about Excel processs still running after the Appliction.Quit was called. Thought I might be able to use the same...
24
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! I have a hidden input field in a form which I change in some occasions on the client using javascript, but when I use "view source" I can't see these changes reflected on the page!...
0
ADezii
by: ADezii | last post by:
Most Access Users realize that Recordsets, being virtual representations of a Query, Table, or SQL Statement, exist only in our PC's memory. They, and the data they contain, literally exist at one...
1
by: Allie | last post by:
Hi, all. This might be a silly question... but I am very new to programming in SQL so please bear with me :) So. I'm using MS SQL Server 2005 Management Studio Express. I have a table that...
16
by: vikas000000a | last post by:
Hello all, I am creating a report generation program with VB 6.0 as front-end and Oracle 10i as back-end. My reqiurements are 1. User will query the database through the front-end. 2. The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.