?
Solved

Richtextbox text color

Posted on 2008-06-20
5
Medium Priority
?
1,040 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

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 2000 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 86

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

777 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