VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Add Colored Text to RichTextbox

by Kamilche (35 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (13 Votes)

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 VBC.

Rate Add Colored Text to RichTextbox

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

Download this snippet    Add to My Saved Code

Add Colored Text to RichTextbox Comments

No comments have been posted about Add Colored Text to RichTextbox. Why not be the first to post a comment about Add Colored Text to RichTextbox.

Post your comment

Subject:
Message:
0/1000 characters