Question Richtextbox color not selected

softhard

Active member
Joined
Sep 29, 2012
Messages
35
Programming Experience
Beginner
Hi,
I would like to display my text in four different colors based on a condition. I have implemented a peice of code for this operation but it is not working instead i got a exception message which i don't understand.
I got this exception from a function where i had written for colored text and i run my code without this color selection which is working fine. However, i have not got any colored text instead i got an exception. Can some help me where i am doing mistake.

Thank you.

The function is here..
Private Sub OnDataReceive(ByVal DataString As String)
Dim Buff As String
Dim DataStr As String = ""
Try
theDate = DateTime.Now
Buff = DataString
For i = 1 To Buff.Length
DataStr &= (
"<" & Hex(Asc(Mid("" & Buff, i, 1))) & ">")
Next
If Convert.ToByte(Asc(Mid(DataStr, 11, 12))) = 0 Then
RichTextBox1.SelectionColor = Color.Blue
ElseIf Convert.ToByte(Asc(Mid(DataStr, 11, 12))) = 1 Then
RichTextBox1.SelectionColor = Color.Red
ElseIf Convert.ToByte(Asc(Mid(DataStr, 11, 12))) = 2 Then
RichTextBox1.SelectionColor = Color.Black
ElseIf Convert.ToByte(Asc(Mid(DataStr, 11, 12))) = 3 Then
RichTextBox1.SelectionColor = Color.Pink
End If
RichTextBox1.AppendText(theDate & ":- " & DataStr & Environment.NewLine)
Catch ex As Exception
MsgBox(ex.Message & "From OnDataReceive function")
End Try
End Sub
 

Attachments

  • namnlös.JPG
    namnlös.JPG
    11.2 KB · Views: 30
Never mind it, i know now what caused this exception. But, still i have not got any colord text yet except black!
 
I have been trying to color certain text in my receiving serial data. The data is continously append to the previous text in Richtextbox. Everytime i receive minimum morethan 20bytes. I would like to color a certain text in the received data. This text is not always same and incoming byte length may vary all the time. I tried the following way but it is coloring all the text and highlighing with a blue color.

RichTextBox1.AppendText(theDate & ":- " & DataStr & Environment.NewLine)
If RichTextBox1.TextLength > 20 Then
RichTextBox1.Select(15, (RichTextBox1.Text.Length - 15))
RichTextBox1.SelectionColor =
Color.Red
End If
 
Do you know what this line is actually doing?
VB.NET:
RichTextBox1.Select(15, (RichTextBox1.Text.Length - 15))
It's selecting from after the fifteenth character to the end of the text. Is that really what you want? If you want to colour the text you just appended then you logical thing to do is:

1. Get the current TextLength, which will be the start index of the text you're about to append.
2. Append the new text.
3. Get the current TextLength, which will be the end index of the text you're about to append.
4. Subtract the start from the end to get the length of the text.
5. Set the SelectionStart and SelectionLength properties explicitly or by calling Select.
6. Set the SelectionColor to colour the text you just appended and selected.
 
Back
Top