Results 1 to 6 of 6

Thread: RichTextBox Control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    Dallas, TX
    Posts
    89

    Post

    How do I change part of a RTB control's text color? For instance
    <Andy> hello
    <Andy> how are you?
    I just want to change <Andy> to a different color on each new line?

  2. #2
    Junior Member
    Join Date
    Nov 1999
    Posts
    16

    Post

    You can do something like this:

    rtb.SelStart = 1 'starting position
    rtb.SelLength = 5 'length
    rtb.SelColor = 255 'your color
    rtb.SelLength = 0 'remove the selection

    You can use InStr() function to determine starting position and length of your part of the text programmatically, if you need.

    Regards, Vlad

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2000
    Location
    Dallas, TX
    Posts
    89

    Post

    vlad

    I tried that code, and it works fine, but only for the first occurrence of <Andy> how do i do it for all occurrences of <Andy>?

    Thanks...

    ande211

  4. #4
    Member
    Join Date
    Jan 2000
    Location
    Glasgow, MT, USA
    Posts
    44

    Post

    You can treat is as an HTML code, There is a code on this site somewhere, that Converts < > and everything in between into another color. Look for HTMLTag.

  5. #5
    Lively Member
    Join Date
    Oct 1999
    Posts
    101

    Post

    Hi Ande.

    Here's code that does what you want (i.e. change <Andy> to red.)

    Name your rich text box control rtb and put a command button on your form.

    Paste this code into the VB Editor:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim stTemp As String
        Dim bEnd As Boolean
        stTemp = "<Andy>"
        
        Do Until InStr(rtb.Text, stTemp) = 0
            If Len(stTemp) = Len(rtb.Text) Then
                If Right(stTemp, 6) <> "<Andy>" Then
                    Exit Sub
                Else
                    bEnd = True
                End If
            End If
            
            With rtb
                .SelStart = Len(stTemp) - 6
                .SelLength = 6
                .SelColor = 255
            End With
            
            If bEnd = False Then
                Do
                    stTemp = stTemp & Mid(rtb.Text, Len(stTemp) + 1, 1)
                Loop Until Right(stTemp, 6) = "<Andy>" Or Len(stTemp) = Len(rtb.Text)
            Else
                Exit Sub
            End If
        Loop
    End Sub
    
    Private Sub Form_Load()
        rtb.Text = "<Andy> Hello" & vbCrLf & "<Andy> How are <Andy>you today? <Andy>"
    End Sub
    All the best.

    Chris

    [This message has been edited by ChrisJackson (edited 01-24-2000).]

  6. #6
    Junior Member
    Join Date
    Jan 2000
    Location
    USA
    Posts
    26

    Post

    hey, I had the exact same problem, and I found a very simple answer.

    say you want to send certian text to the rtb, like when <Joe> says something...

    with RTB1
    .selstart = len(.text)
    .selcolor = vbBlue
    .seltext = "<Joe> "
    .selstart = len(.text)
    .selcolor = vbRed
    .seltext = " hey this works great!"
    end with

    you can easily make a sub for it so you can do something like Call Color(vbRed,"Hey") or something like that, thats what I did.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width