Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Different text colors in richtextbox .

Posted on 2003-10-22
4
Medium Priority
?
748 Views
Last Modified: 2012-06-27
i have a piece of code  ,

Private Sub StatusLog(What As String)

TimeNow = Time()


If txtStatus.Text = "" Then
 
    txtStatus.SelText = "[ " & TimeNow & " ] --- " & What
Else
    txtStatus.Text = txtStatus.Text & vbCrLf
    txtStatus.Text = txtStatus.Text & "[ " & TimeNow & " ] --- "
    txtStatus.Text = txtStatus.Text & What
End If

End Sub

where if Statuslog "AAA " , then it'll display ---- [ 3:00 PM ] AAA

how can i do that [ 3:00 PM ] is in red color and "AAA" is in green color ?
0
Comment
Question by:vb_newbie
[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
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9600652
What a pain trying to figure this one out!  Here is your answer:

Private Sub StatusLog(What As String)
    With txtStatus
        .SelLength = 0 ' make sure no text is currently selected
        .SelStart = Len(txtStatus.Text) ' make sure we are appending
        .SelColor = QBColor(4) ' change color of new text to red
        .SelText = "[ " & Time() & " ] --- " ' append time tag
        .SelColor = QBColor(2) ' change color of nex text to green
        .SelText = What & vbCrLf ' append what tag and vbcrlf
    End With
End Sub

Problems occur whenever you use this type of statement
     txtStatus.Text = txtStatus.Text  & something
because using the .text property only returns the TEXT without the RTF encoding resulting in the original contents of the richtextbox colored in whatever color was last used.

Hope this helps!
0
 

Author Comment

by:vb_newbie
ID: 9601530
well that's the main problem , txtStatus.Text = txtStatus.Text  & something
if without above the line , then how to save the states continuosly ?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 160 total points
ID: 9601590
Create a new project.  Add a richtextbox control, a textbox, and a commandbutton.

Paste the following into your form code and then type something in the textbox and hit the button.  Repeat as many times as desired. =)

Private Sub StatusLog(What As String)
    With RichTextBox1
        .SelLength = 0 ' make sure no text is currently selected
        .SelStart = Len(RichTextBox1.Text) ' make sure we are appending
        .SelColor = QBColor(4) ' change color of new text to red
        .SelText = "[ " & Time() & " ] --- " ' append time tag
        .SelColor = QBColor(2) ' change color of nex text to green
        .SelText = What & vbCrLf ' append what tag and vbcrlf
    End With
End Sub

Private Sub Command1_Click()
    StatusLog (Text1.Text)
End Sub

Private Sub Form_Load()
    RichTextBox1.Text = ""
    Text1.Text = ""
End Sub
0
 

Author Comment

by:vb_newbie
ID: 9606341
it works . thanks .
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

670 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