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?
Printable View
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?
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
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
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.
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:All the best.Code: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
Chris
[This message has been edited by ChrisJackson (edited 01-24-2000).]
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.