?
Solved

Rich Text Box Color help !

Posted on 2003-10-25
7
Medium Priority
?
292 Views
Last Modified: 2010-07-27
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 .
0
Comment
Question by:ANGmoh
[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
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 9619632
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!
0
 
LVL 1

Author Comment

by:ANGmoh
ID: 9620030
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 !
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9620045
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!... =)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ANGmoh
ID: 9620080
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 :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9620097
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. =)
0
 
LVL 1

Author Comment

by:ANGmoh
ID: 9620110
timePosition = InStr(lastPosition, contents, "]") <-- this will get the lenght of [ 9:00:00PM ] ? i think yes .
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9620195
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!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses

649 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