Solved

Richtextbox text color

Posted on 2008-06-20
5
1,027 Views
Last Modified: 2008-09-12
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

0
Comment
Question by:emresamisuzer
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 21832301
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

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21832490
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

0
 

Author Comment

by:emresamisuzer
ID: 21836666
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
0
 
LVL 48

Accepted Solution

by:
jpaulino earned 500 total points
ID: 21836830
You can change it to this:
    Private Sub myworkerclass_AddDateTime(ByVal message As String, ByVal thecolor As System.Drawing.Color) Handles myworkerclass.AddDateTime

        With Me.rtb_messages

            .SelectedText = IIf(.SelectedText = String.Empty, message & vbNewLine, message & vbNewLine & .SelectedText)

            .Select(.Find(message), message.Length)

            .SelectionColor = thecolor

        End With

    End Sub

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21838228
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

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 46
Visual Studio: built-in keystroke automation 2 26
Round up to 100% in .NET 10 46
Birthdays 3 21
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now