[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

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 .
0
ANGmoh
Asked:
ANGmoh
  • 4
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
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 !
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Technology Partners: 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!

 
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 :)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
ANGmohAuthor Commented:
timePosition = InStr(lastPosition, contents, "]") <-- this will get the lenght of [ 9:00:00PM ] ? i think yes .
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now