Solved

Rich Text Box Color help !

Posted on 2003-10-25
7
279 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
  • 4
  • 3
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 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 85

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 85

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 85

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

707 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

16 Experts available now in Live!

Get 1:1 Help Now