IHTMLTxtRange problem using Webbrowser

Asked By Soft DEveloper
20-Nov-09 04:46 AM
Earn up to 0 extra points for answering this tough question.

Hello,

 I have used Webbrowser control in editable mode.I am using IHTMLTxtRange for find a word and replacing it with other word.I have to find and replace internally in my code.

Following is my code:-

Public Sub FindReplaceText(ByVal sfindText As String, ByVal sReplacetext As String)
Try


FindReplaceReset()
FindReplaceAll(sfindText, sReplacetext, False, False)
Catch ex As Exception

End Try
End Sub



Private Sub FindReplaceReset()


document = DirectCast(HTMLEditor.Document.DomDocument, IHTMLDocument2)
body = DirectCast(document.body, HTMLBody)

' reset the range being worked with
_findRange = DirectCast(body.createTextRange(), IHTMLTxtRange)
DirectCast(document.selection, IHTMLSelectionObject).empty()

End Sub
' replaces all the occurrences of the given string with the other
Private Function FindReplaceAll(ByVal findText__1 As String, ByVal replaceText As String, ByVal matchWhole As Boolean, ByVal matchCase As Boolean) As Integer
Dim found As Integer = 0
Dim replaceRange As IHTMLTxtRange = Nothing

Do
' find the given text within the find range
replaceRange = FindText(findText__1, matchWhole, matchCase)
' if found perform a replace
If replaceRange IsNot Nothing Then
replaceRange.text = replaceText
found += 1
End If
Loop While replaceRange IsNot Nothing

' return count of items repalced

Return found
End Function
'FindReplaceAll
' function to perform the actual find of the given text
Private Function FindText(ByVal sfindText As String, ByVal matchWhole As Boolean, ByVal matchCase As Boolean) As IHTMLTxtRange
' define the search options
Dim searchOptions As Integer = 0
If matchWhole Then
searchOptions = searchOptions + 2
End If
If matchCase Then
searchOptions = searchOptions + 4
End If

If Not _findRange.text Is Nothing Then


' perform the search operation
If _findRange.findText(sfindText, _findRange.text.Length, searchOptions) Then
' select the found text within the document
_findRange.[select]()
' limit the new find range to be from the newly found text
Dim foundRange As IHTMLTxtRange = DirectCast(document.selection.createRange(), IHTMLTxtRange)
_findRange = DirectCast(body.createTextRange(), IHTMLTxtRange)
_findRange.setEndPoint("StartToEnd", foundRange)
' text found so return this selection
Return foundRange
Else
' reset the find ranges
FindReplaceReset()
' no text found so return null range
Return Nothing

End If
Else
' reset the find ranges
FindReplaceReset()
' no text found so return null range
Return Nothing
End If
End Function

  It works fine for all find and replace except if the word contains vbnewline.If in webbrowser control I enter

Hiii

Hello.

 The above two words contain Enter key(i.e vbnewline) between them.If I pass followinf string to my Method it doesn't recognize the whole word and doesn't replace the word. I have called my method as 

FindReplaceAll("Hii"+vbnewline+"Hello","HiiiHelloReplaced", False, False)

  Can anyone tell me why is it so?



  Do this.

[)ia6l0 iii replied to Soft DEveloper
22-Nov-09 12:17 PM
I am not sure how the webbrowser or the IHTMLTxtRange treats the newline characters. But what if you get it from the webbrowser itself?

I presume that your webbrowser control is called "webbrowser1". Attach an onSelectionChanged event to the document of the webbrowser. and you will be able to figure out the actual stuff behind the newline text.

Attach the onSelectionChanged event in the documentcompleted event of the webbrowser control like:
Private Sub webbrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    webbrowser1.Document.AttachEventHandler("onSelectionChanged", New EventHandler(OnSelectStart))
End Sub

And when you select any text after this, the OnSelectionChange event would fire, which would give you the selected text. Yes, you have to select the "Hiii Newline Hello." text.

and this is how the onSelectionChange should look:

Private Sub OnSelectionChange(ByVal sender As Object, ByVal e As EventArgs)
    Dim doc As IHTMLDocument2 = DirectCast(webBrowser.Document.DomDocument, IHTMLDocument2)
    Dim selection As IHTMLSelectionObject = doc.selection
    Dim selectedRange As IHTMLTxtRange = DirectCast(selection.createRange(), IHTMLTxtRange)
    'The selected Text
    Dim selectedText As String = selectedRange.text
End Sub

Use the SelectedText to do your Find-Replace.

Hope this helps.
Create New Account