Search Tools Links Login

Add Colored Text to RichTextbox


Adds the text to a textbox, checking for length overflow. First, it checks, and if the textbox exceeds 15,000 characters, it strips out all but the last 2000 characters to make room for the new text. It doesn't break in the middle of lines - it only deletes 'whole lines.' Then it adds the text to the end, using the color you specified (if any), and scrolls to the end of the textbox. I use it all the time, got tired of cutting/pasting out of old projects, thought I'd put it here on PSC.

Original Author: Kamilche

Code

Public Sub Display(ByVal s As String, Optional Color As Long = vbGreen)
  'Add text to the text output window.
  With frmMain.RichTextBox1
    'Clear all but the last 2000 characters if it's too large
    '(don't cut it off in the middle of a line tho).
    If Len(.Text) + Len(s) > 15000 Then
      .SelStart = 0
      .SelLength = InStrRev(.Text, vbCrLf, Len(.Text) - 2000, vbTextCompare) + 1
      .SelText = ""
    End If
    .SelStart = Len(.Text)
    .SelColor = Color
    .SelText = s & vbCrLf
    .SelStart = Len(.Text)
  End With
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 111 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.