Link to home
Start Free TrialLog in
Avatar of emresamisuzer
emresamisuzer

asked on

Richtextbox text color

I need to change the color of the added line color, but after three repetitions all line colors become the same.

You may see the function that adds new line to the richtextbox and assigns color to it.

When I run it four times with the settings you may see except the first line all lines become red.
Private Sub myworkerclass_AddDateTime(ByVal message As String, ByVal thecolor As System.Drawing.Color) Handles myworkerclass.AddDateTime
        Me.rtb_messages.Text = message & vbCrLf & Me.rtb_messages.Text
        Me.rtb_messages.Select(0, message.Length)
        Me.rtb_messages.SelectionColor = thecolor
    End Sub
 
        myworkerclass_AddDateTime("Test1!", Color.Purple)
        myworkerclass_AddDateTime("Test2!", Color.Red)
        myworkerclass_AddDateTime("Test3!", Color.YellowGreen)
        myworkerclass_AddDateTime("Test4!", Color.Tomato)

Open in new window

Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Hi,

Try to change you sub to this:

    Private Sub myworkerclass_AddDateTime(ByVal message As String, ByVal thecolor As System.Drawing.Color) Handles myworkerclass.AddDateTime
        Me.rtb_messages.AppendText(message + vbNewLine)
        Me.rtb_messages.Select(Me.rtb_messages.Find(message), message.Length)
        MsgBox(Me.rtb_messages.SelectedText)
        Me.rtb_messages.SelectionColor = thecolor
    End Sub

Open in new window

You can also do it this way:
    Private Sub myworkerclass_AddDateTime(ByVal message As String, ByVal thecolor As System.Drawing.Color) Handles myworkerclass.AddDateTime
        Me.rtb_messages.SelectionStart = Me.rtb_messages.TextLength
        Me.rtb_messages.SelectionColor = thecolor
        Me.rtb_messages.SelectedText = message & vbCrLf
    End Sub

Open in new window

Avatar of emresamisuzer
emresamisuzer

ASKER

Thanks for your responses but I need the appended text to be appended at the top line of the richtextbox. That is why I use the code:
Me.rtb_messages.Text = message & vbCrLf & Me.rtb_messages.Text
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mine would be:
    Private Sub myworkerclass_AddDateTime(ByVal message As String, ByVal thecolor As System.Drawing.Color) Handles myworkerclass.AddDateTime
        Me.rtb_messages.SelectionStart = 0
        Me.rtb_messages.SelectionLength = 0
        Me.rtb_messages.SelectionColor = thecolor
        Me.rtb_messages.SelectedText = message & vbCrLf
    End Sub

Open in new window