PDA

Click to See Complete Forum and Search --> : RichTextBox Control


ande211
Jan 24th, 2000, 04:53 AM
How do I change part of a RTB control's text color? For instance
<Andy> hello
<Andy> how are you?
I just want to change <Andy> to a different color on each new line?

vladimir
Jan 24th, 2000, 05:20 AM
You can do something like this:

rtb.SelStart = 1 'starting position
rtb.SelLength = 5 'length
rtb.SelColor = 255 'your color
rtb.SelLength = 0 'remove the selection

You can use InStr() function to determine starting position and length of your part of the text programmatically, if you need.

Regards, Vlad

ande211
Jan 24th, 2000, 05:37 AM
vlad

I tried that code, and it works fine, but only for the first occurrence of <Andy> how do i do it for all occurrences of <Andy>?

Thanks...

ande211

Phil.Hebert
Jan 24th, 2000, 07:07 AM
You can treat is as an HTML code, There is a code on this site somewhere, that Converts < > and everything in between into another color. Look for HTMLTag.

ChrisJackson
Jan 24th, 2000, 07:17 AM
Hi Ande.

Here's code that does what you want (i.e. change <Andy> to red.)

Name your rich text box control rtb and put a command button on your form.

Paste this code into the VB Editor:
Option Explicit

Private Sub Command1_Click()
Dim stTemp As String
Dim bEnd As Boolean
stTemp = "<Andy>"

Do Until InStr(rtb.Text, stTemp) = 0
If Len(stTemp) = Len(rtb.Text) Then
If Right(stTemp, 6) <> "<Andy>" Then
Exit Sub
Else
bEnd = True
End If
End If

With rtb
.SelStart = Len(stTemp) - 6
.SelLength = 6
.SelColor = 255
End With

If bEnd = False Then
Do
stTemp = stTemp & Mid(rtb.Text, Len(stTemp) + 1, 1)
Loop Until Right(stTemp, 6) = "<Andy>" Or Len(stTemp) = Len(rtb.Text)
Else
Exit Sub
End If
Loop
End Sub

Private Sub Form_Load()
rtb.Text = "<Andy> Hello" & vbCrLf & "<Andy> How are <Andy>you today? <Andy>"
End Sub

All the best.

Chris

[This message has been edited by ChrisJackson (edited 01-24-2000).]

clonE
Jan 24th, 2000, 09:17 AM
hey, I had the exact same problem, and I found a very simple answer.

say you want to send certian text to the rtb, like when <Joe> says something...

with RTB1
.selstart = len(.text)
.selcolor = vbBlue
.seltext = "<Joe> "
.selstart = len(.text)
.selcolor = vbRed
.seltext = " hey this works great!"
end with

you can easily make a sub for it so you can do something like Call Color(vbRed,"Hey") or something like that, thats what I did.