Finding and Replacing
You can find text in the Rich Text box control very easily using the Find Method.
The Find Method uses the following syntax:
RichTextBox1.Find FindString, StartPos, EndPos,
Options
FindString is the Text to fine. StartPos is an integer representing
the start position. To start from the beginning of the document this would be
set to 0. EndPos is an integer representing the end position. If this
is left blank then it is set the the length of the document. Options contains
one or more of the following flags:
|
Flag
|
Action
|
| rtfNoHighlight |
Don't highlight selection |
| rtfWholeWord |
Only searches for the whole word |
| rtfMatchCase |
Matches the case of the FindString |
The following code searches RichTextBox1 for the Text contained in Text1. It
searched for the whole word only:
Sub Command1_Click()
SearchText = Text1.Text
gOptions = rtfWholeWord
Result = RichTextBox1.Find (SearchText,0, ,gOptions)
If Result = -1 Then
Msgbox "Text not found"
Else
Msgbox "Text Found"
End If
End Sub
Once you have found the text, you can change it using the SelText property.
The following code will search the whole of RTB1 for the text contained in txtFind,
and then will replace that text with the text in txtReplace. It uses Match Case
and Whole Word Only.
' Set the options
gOptions = rtfWholeWord + rtfMatchCase
' Set the counter to 0
gCount = 0
Do
If gDoc.Find(txtFind.Text, RTB1.SelStart + RTB1.SelLength,
, goptions) = -1 Then
' No more matches
If gCount = 0 Then
' First time
round, no matches found
MsgBox "Unable
to find '" & txtFind.Text & "' in specified range"
Else
' Matches
found
MsgBox "The
specified region has been searched. " & gCount & _
" replacements have been made"
End If
cmdFind.Caption = "&Find"
Exit Do
Else
' Increment replace count
gCount = gCount + 1
' Replace the selected text with
the replace text
gDoc.SelText = txtReplace.Text
End If
Loop
-
Posted by mmarksbury on 24 Apr 2008
This is long past the topic post date but...
I used the selection method (where you select text and then update it), for formatting the output of an application's process in a RTF. ...
-
Posted by magiel on 06 Jan 2007
I am had the same proplems;
RichTextBox1.SelectionFont.Bold
RichTextBox1.SelectionFont.Underline etc are readonly
Solved them the following way
... -
Posted by Verdi on 21 Sep 2006
Kylua, your efforts were not wasted...
You have probably saved me a week's worth of head-pounding. Why it is so difficult to programmatically modify RTF using this control is beyond me. All...
-
Posted by Kylua on 31 Jul 2006
I spent AGES working thru this one and couldn't find it anywhere!
The basic problem is that you have to update the richtextbox.rtf, not .text.
And it is very fussy about goes in the...
-
Posted by vb_lover on 06 Dec 2005
James Crowley:
I am using vb6sp6(EM_SETTEXTMODE didn't work with vb6sp4 either) RTB control, I have already implemented URL detection similar to yours. However now i am trying to get MULTI-LEVEL U...