HtmlEditor :  Phorum 5 The fastest message board... ever.

This is the discussion forum for the HtmlEditor. See also the HtmlEditor home page, where you can download the control, and the Documentation Wiki, a collaborative project for documenting the control.

Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
About find and replace in HTMlEditor
Posted by: Prachi (117.241.253.---)
Date: Saturday, 21-Nov-2009, 12:10:29

Hello,
In my code I want to internally find a particular word and replace it with other.
I have used Webbrowser control in editable mode.I am using IHTMLTxtRange for finding 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 following 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"+vbcr+"Hello","HiiiHelloReplaced", False, False)

Can anyone tell me why is it so?



Your Name: 
Your Email: 
Subject: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
Ld9FW
This is a moderated forum. Your message will remain hidden until it has been approved by a moderator or administrator
This forum powered by Phorum.