Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rich Text Box Quickie? 1

Status
Not open for further replies.

wraygun

Programmer
Dec 9, 2001
272
US
I'm using the replace function During the 'Change' Event of a RichTextBox. I'm wanting it to look for certain keywords in the body of the text and then Capitalize them or Change the Color(on the fly). I've had success if I do it from a command button, but if I do it (on the fly) I get bizarre results.

What I have so far:


Private Sub RTB_Change()

RTB.Text = Replace(RTB.Text, "bob", "BOB")

End Sub


The text is returned to the box in reverse order. I tried the 'StrReverse' function, but that's when it got really strange.

I think what is happening is that when the new string is inserted into RTB.Text, that the cursor position remains in front of the text, rather than in the back, so it may be as simple as telling the cursor to go to the end of the text. However, I lack the knowhow to make that happen. Any help will be appreciated?

Thanks,
The Gun
 

Try remembering where the caret is before you use the replace function. Then add the length of the replaced word to you original caret position.

Something like (and I've not checked this)

ToFind = "bob"

If InStr(1, RichTextBox1.Text, LCase(ToFind)) > 0 Then
OriginalPos = RichTextBox1.SelStart
RichTextBox1.Text = Replace(RichTextBox1.Text, LCase(ToFind), UCase(ToFind))
RichTextBox1.SelStart = OriginalPos + Len(ToFind)
End If

Not a complete answer but It'll keep you moving on !!

Dazzer
 
Thank you. That's exactly the boost I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top