Solved

Different text colors in richtextbox .

Posted on 2003-10-22
4
743 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 85

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 85

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

820 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