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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I change the color of a rich text box control?

Status
Not open for further replies.

Hawkeye123456

Programmer
May 5, 2000
24
0
0
US
This is the code that I have right now

and txtstuff is a rich text box

Private Sub client_DataArrival(ByVal bytesTotal As Long)

Dim stuff() As String, i As Integer

Dim str As String
client.GetData str
stuff = Split(str, Chr(255))
For i = 0 To UBound(stuff)
If Trim(stuff(i)) <> &quot;&quot; Then
txtStuff.SelColor = RGB(0, 0, 255)
txtStuff.SelStart = 1 'Len(txtStuff)
txtStuff.Text = txtStuff.Text &amp; stuff(i) &amp; vbCrLf
End If

Next i

End Sub
 
Try setting the SelLength property of the RichTextBox control, and setting the SelColor property AFTER setting the SelStart and SelLength properties.

To get it to work, try the following:
Add a RichTextBox control and a Command button to a form. Name the RichTextBox control txtRTF.
Add the code below to the Click event of the button:

Private Sub Command1_Click()
txtRTF.SelStart = 0
txtRTF.SelLength = 5
txtRTF.SelColor = RGB(0, 0, 255)
txtRTF.SelStart = 5
txtRTF.SelLength = 5
txtRTF.SelColor = RGB(0, 255, 0)
End Sub


Run the program and make sure the RichTextBox control has at least 10 characters in it, then click Command1.
The first 5 characters should turn blue and the second 5 characters should turn green.

PS - questions about your code:
1. Do you mean to start the highlighting after the first character?? (Setting the SelStart property to 1 will miss the first character, whereas setting the SelStart property to 0 will include the first character).
2. Why do you set the SelStart and SelColor properties inside the loop?? Wouldn't it be better to use the loop to append all of the data, then after the loop check to see if there is anything in the RichTextBox (if this is valid) and then set the SelStart, SelLength and SelColor (in that order) properties??

Simon
 
That would work except MSDN said


Remarks

If there is no text selected in the RichTextBox control, setting this property determines the color of all new text entered at the current insertion point.


and I dont want to see the selection flash like it would using that method.

Thanks though
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top