How can I add multi-colored text at run-time to a rich text box using code without destroying its existing content? I don’t mean loading a file, but adding strings to it. The rtf box is locked and used for display only.
Printable View
How can I add multi-colored text at run-time to a rich text box using code without destroying its existing content? I don’t mean loading a file, but adding strings to it. The rtf box is locked and used for display only.
Hello bkspace,
You will need to parse the text in the richtextbox for the words you want to change the color for.
Once you find a word you then would use the the selstart, sellen and selcolor proporties to change the color of the word.
As such:
Hope this helps a little,Code:
Private Sub Command1_Click()
Dim I As Long, lRtn As Long
Dim sSrchText As String
sSrchText = "Word" ' text to look for
For I = 1 To Len(rtb1.Text) ' set up a loop to search the text
lRtn = InStr(I, rtb1.Text, sSrchText, vbTextCompare) 'search for the text
If lRtn > 0 Then ' if you found something change the color
rtb1.SelStart = lRtn - 1
rtb1.SelLength = Len(sSrchText)
rtb1.SelColor = QBColor(2)
I = lRtn ' normally you would not do this but it does speed things up
Else
Exit For ' if you don't find anymore exit the loop
End If
Next I
End Sub
Roger
[Edited by RvA on 04-16-2000 at 04:26 PM]