Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Different text colors in richtextbox .

Posted on 2003-10-22
4
Medium Priority
?
749 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

876 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