Results 1 to 9 of 9

Thread: [RESOLVED] Coloring text in Rich Text Box for chat program.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    80

    Resolved [RESOLVED] Coloring text in Rich Text Box for chat program.

    I have a rich text box that displays text from a chat channel. So each time text is send to the channel it's added, like this:

    rtbText.text = rtbText.text + Incoming
    rtbText.SelStart = Len(rtbText.Text)

    I want to give a color to the Incoming string and this piece of text will stay colored in the Rich Text Box and will not be changed after a new message has been colored in a different color.

    How can this be done?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Coloring text in Rich Text Box for chat program.

    If the string never changes color, won't you in very short order, run out of colors?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    80

    Re: Coloring text in Rich Text Box for chat program.

    nah, there are 16 million, right

    let me explain:
    the Incoming string contains a message that is added to the RTB (with all messages), so it contains different text each time a new message comes in.

    The message will be colored if its a join message or quit message, for example.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Coloring text in Rich Text Box for chat program.

    The best way to do it is when you receive the message. Here's an example.

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     With RichTextBox1
    4.         .SelStart = Len(.Text) 'Go to end.
    5.         .SelBold = True 'Bold for username.
    6.         'Set color to blue for my username, red for someone else.
    7.         .SelColor = IIf(Username = MyUsername, vbBlue, vbRed)
    8.         .SelText = Username 'Enter their username.
    9.         .SelColor = 0 'Black.
    10.         .SelText = ": "
    11.        
    12.         'Message text.
    13.         .SelColor = RGB(0, 123, 0) 'Green text?
    14.         .SelBold = False
    15.         .SelText = Message & vbCrLf
    16.     End With
    17.    
    18. End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    80

    Re: Coloring text in Rich Text Box for chat program.

    ty,
    your code colors the nick and the last message that has come in.
    but the color of this particular line dissappears when a new message comes in, becuase the new message will be colored.

    it's always the last line that has colors

    it would be nice if the colors of the 1st messages stay, when new messages come in

  6. #6
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Coloring text in Rich Text Box for chat program.

    That should not happen. Can you post your code?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    80

    Re: Coloring text in Rich Text Box for chat program.

    VB Code:
    1. Public Sub ChannelIn()
    2.         rtbChannel.Text = rtbChannel.Text + Incoming & vbCrLf
    3. With rtbChannel
    4.         .SelStart = Len(.Text) 'Go to end.
    5.         .SelBold = True 'Bold for username.
    6.         'Set color to blue for my username, red for someone else.
    7.         .SelColor = IIf(Nick = OwnNick, vbBlue, vbRed)
    8.         .SelText = Nick 'Enter their username.
    9.         .SelColor = 0 'Black.
    10.         .SelText = ": "
    11.        
    12.         'Message text.
    13.         .SelColor = RGB(0, 123, 0) 'Green text? yes
    14.         .SelBold = False
    15.         .SelText = Incoming & vbCrLf
    16.     End With
    17.     rtbChannel.SelStart = Len(rtbChannel.Text) 'keeps incoming text at the end of textbox
    18. End Sub

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Coloring text in Rich Text Box for chat program.

    Remove or comment out the first line.

    VB Code:
    1. [COLOR=Red]rtbChannel.Text = rtbChannel.Text + Incoming & vbCrLf[/COLOR]

    Also, if you do use something like this, always use & instead of + for strings. + is for numerical stuff, like adding 2 numbers. & is for adding one string to another.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2007
    Location
    Netherlands
    Posts
    80

    Re: Coloring text in Rich Text Box for chat program.

    thanks, now all lines have color, not only the last

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