Solved

Different text colors in richtextbox .

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now