Rich Text Box Color help !

i have a piece of code which will look like this

Private Sub StatusLog(What As String)
   With txtStatus  ' <--- rich text box
      .SelLength = 0
      .SelStart = Len(txtStatus.Text)
      .SelColor = Configuration.picStatusColor(2).BackColor
      .SelText = "[ " & Time() & " ] " 
      .SelColor = Configuration.picStatusColor(1).BackColor
      .SelText = What & vbCrLf
  End With
End Sub

Private Sub Form_Load ()
   StatusLog "Welcome.."
End Sub

Private Sub printmsg()
   StatusLog "blahblah"
End Sub

The users may change the color which they want from another configuration form . my problem is  when the users have changed the color , how can i apply the color changes to all of the text in the txtStatus ?

It might look like this , in txtStatus


[ 9:00:01 PM ] Welcome welcome <--- before color make changes  (blue)
[ 9:00:02 PM ] Welcome <--(blue)
[ 9:00:03 PM ] blah blah blah <----user changes the font color (red)
[ 9:00:04 PM ] blah blha <---- (red)

what i wan to do is to  change the blue text into red too ..


thanks .
LVL 1
ANGmohAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Well, to change the ENTIRE contents to a different color you could use:

Private Sub changeColorTo(newColorCode As Long)
    With txtStatus  ' <--- rich text box
        .SelStart = 0
        .SelLength = Len(txtStatus.Text)
        .SelColor = QBColor(newColorCode)
    End With
End Sub

and call it with:

changeColor(QBColor(2))

However, it looks like your have two diferent colors in each line of your text.  You could use this code to change to accomplish two colors:

Private Sub changeColorsTo(timeStampColor As Long, msgColor As Long)
    Dim contents As String
    Dim markerPosition As Long
    Dim lastPosition As Long
    Dim timePosition As Long
   
    contents = txtStatus.Text
    lastPosition = 1
    Do
        markerPosition = InStr(lastPosition, contents, "" & vbCrLf)
        If markerPosition > 0 Then
            timePosition = InStr(lastPosition, contents, "]")
            With txtStatus
                .SelLength = 0
                .SelStart = lastPosition
                .SelLength = timePosition - lastPosition + 1
                .SelColor = timeStampColor
                .SelLength = 0
                .SelStart = timePosition + 1
                .SelLength = markerPosition - timePosition
                .SelColor = msgColor
                .SelLength = 0
            End With
            lastPosition = markerPosition + 1
        End If
    Loop While markerPosition > 0
End Sub

and then call it like this:

changeColorsTo Configuration.picStatusColor(2).BackColor Configuration.picStatusColor(1).BackColor

Hope it works for you!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ANGmohAuthor Commented:
well , it works ! but with a little bug ...
 the first character's color is not changing .. so i make a slightly change to the code ..

Private Sub changeColorsTo(timeStampColor As Long, msgColor As Long)
    Dim contents As String
    Dim markerPosition As Long
    Dim lastPosition As Long
    Dim timePosition As Long
   
    contents = Server.txtStatus.Text
    lastPosition = 1
    Do
        markerPosition = InStr(lastPosition, contents, "" & vbCrLf)
        If markerPosition > 0 Then
            timePosition = InStr(lastPosition, contents, "]")
            With Server.txtStatus
                .SelLength = 0
                .SelStart = lastPosition - 1
                .SelLength = timePosition - lastPosition + 2
                .SelColor = timeStampColor
                .SelLength = 0
                .SelStart = timePosition + 1
                .SelLength = markerPosition - timePosition
                .SelColor = msgColor
                .SelLength = 0
            End With
            lastPosition = markerPosition + 1
        End If
    Loop While markerPosition > 0
End Sub

----
cheers !
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Oops....Sorry, you are absolutely right.  Thank You Microsoft once again for being inconsistent!

The Instr function treats the first character as 1.
The RichTextBox treats the first character as 0.

Happy Coding!... =)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ANGmohAuthor Commented:
Private Sub changeColorsTo(timeStampColor As Long, msgColor As Long)
    Dim contents As String
    Dim markerPosition As Long
    Dim lastPosition As Long
    Dim timePosition As Long
   
    contents = Server.txtStatus.Text
    lastPosition = 1
    Do
        markerPosition = InStr(lastPosition, contents, vbCrLf)
         If markerPosition > 0 Then
            timePosition = InStr(lastPosition, contents, "]")
            MsgBox "time pos : " & timePosition
            With Server.txtStatus
                .SelLength = 0
                .SelStart = lastPosition - 1
                .SelLength = timePosition
                .SelColor = timeStampColor
                .SelLength = 0
                .SelStart = timePosition + 1
                .SelLength = markerPosition - timePosition
                .SelColor = msgColor
                .SelLength = 0
            End With
            lastPosition = markerPosition + 1
        End If
    Loop While markerPosition > 0
End Sub

----
 this will be more correct :)
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
ANGmoh, your logic is flawed my friend.

With Server.txtStatus
                .SelLength = 0
                .SelStart = lastPosition - 1
                .SelLength = timePosition      <------------------------  !!???!!!
                .SelColor = timeStampColor
                .SelLength = 0
                .SelStart = timePosition + 1
                .SelLength = markerPosition - timePosition
                .SelColor = msgColor
                .SelLength = 0
End With

The timePosition variable is the position of the next "]" marker beginning from the last vbCrLF found, but its value is relative to the beginning of the string.  You can't use it directly to determine the length of the current time stamp.  You still have to do the calculation. =)
ANGmohAuthor Commented:
timePosition = InStr(lastPosition, contents, "]") <-- this will get the lenght of [ 9:00:00PM ] ? i think yes .
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It does not give you the length of the timestamp!   It gives you the position of the "]" in the string.

YES...

timePosition = InStr(lastPosition, contents, "]")

will in fact give you the length of the timestamp, but ONLY FOR THE VERY FIRST ONE!

The string we are examining is the entire contents of his richtextbox control which will have MANY timestamps/messages in it.

Try this code out to convince yourself of what is really happening:

Private Sub Form_Load()
    Contents = "[8] hi" & vbCrLf & "[999] hello" & vbCrLf & _
        "[8394] bye" & vbCrLf
    lastPosition = 1
    Do
        markerPosition = InStr(lastPosition, Contents, vbCrLf)
         If markerPosition > 0 Then
            timeposition = InStr(lastPosition, Contents, "]")
            Debug.Print timeposition
            lastPosition = markerPosition + 1
        End If
    Loop While markerPosition > 0
End Sub

Cheers!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.