Solved

Different text colors in richtextbox .

Posted on 2003-10-22
4
746 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 40 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Run code from text file in vb 1 102
VB 6 error 5 in windows 10 but not in XP 7 72
VB6 - Scroll Mouse wheel on Picturebox 13 120
Problem to line 23 80
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

738 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