Link to home
Start Free TrialLog in
Avatar of andyakira
andyakira

asked on

How do i do this? Delete Old text from a RTB after a certain amount of length.

Is it possible to delete text from the RTB after a certain length. like how a chatroom would delete the older text on TOP after a certain length without the user knowing.
Avatar of Bob Lamberson
Bob Lamberson
Flag of United States of America image

Hi andyakira,


Dim txtLength as Int
txtLength = 25      ' set the number to whatever length you want.
Private Sub RichTextBox1_Change()
If Len(RichTextBox1.Text) > txtLength Then    
   RichTextBox1.Text = Right(RichTextBox1.Text, txtLength)
   RichTextBox1.Find vbCrLf
End If
End Sub


Bob
Avatar of andyakira
andyakira

ASKER

Thanks bob, this works.  but its deleting all my colors in the RTB.
andyakira,

what colors?

Bob
I have different colors in the RTB, when i use the code. it makes everything 1 color.. this is my add to RTB code:

    Dim i As Integer
With Form1.RTB
End With
        With Form1.RTB
        .SelStart = 99999999
        .SelLength = 0
        .SelColor = vbWhite
        .SelStart = 99999999
        End With
    For i = LBound(saElements) To UBound(saElements) Step 2
        With Form1.RTB
        .SelStart = 99999999
        .SelLength = 0
        .SelColor = saElements(i)
        .SelText = saElements(i + 1) & Left$(vbCrLf, -2 * CLng((i + 1) = UBound(saElements)))
        .SelStart = 99999999
        End With
    Next i


>> what colors?

The Text property of a RTB contains just that; the plain text. None of the RTF tags are retained if you change it. The .TextRTF property has the formatted document.

Sorry, this may be more complicated to accomplish than I thought, I cannot think of how to do this right now... Maybe in the morning :-/

-Burbble
Ick, that...

        .SelStart = 99999999

...is kind of ugly. I think you want to change it to

        .SelStart = Len(.Text)

I will get back to you if I come up with a good solution for this. Another expert will probably have something better by then ;-)

-Burbble
:D thanks
Here is an addon to Bobs code;

Dim txtLength as Int
txtLength = 250      ' set the number to whatever length you want.
Private Sub RichTextBox1_Change()
If Len(RichTextBox1.RTBText) > txtLength Then    
   RichTextBox1.RTBText = Right(RichTextBox1.RTBText, txtLength) '<--- try changing to .RTBText
   RichTextBox1.Find vbCrLf
End If
End Sub


andyakira,

Brian
I tested that change and it crashes vb.

I did get the following to work tho
Option Explicit

Dim txtLength As Integer

Private Sub Form_Load()
RichTextBox1.SelColor = vbRed

End Sub

Private Sub RichTextBox1_Change()
txtLength = 25      ' set the number to whatever length you want.
If Len(RichTextBox1.Text) > txtLength Then
   RichTextBox1.Text = Right(RichTextBox1.Text, txtLength)
   RichTextBox1.Find vbCrLf
End If
End Sub

Private Sub RichTextBox1_SelChange()
RichTextBox1.SelColor = vbRed

End Sub


Bob
Thanks guys for the help but still not working the way i was hoping for.

My  RTB has multiple colors, emotions are yellow, user join/leave is grey etc, kind of like IRC.  I'm looking for a answer that dosn't change the colors of the text.
Avatar of GrahamSkan
I think you'll have to save and restore the colours if you modify the text programatically.

This code sets up a few lines, restoring the colours as necessary.
The Command1_Click event demonstrates shortening the text.

Option Explicit


Private Sub Command1_Click()
Dim j As Integer
Dim l As Integer
Dim Colours() As Long

    l = Len(RichTextBox1.text)
    If l > 0 Then
        ReDim Colours(l - 1)
        For j = 0 To l - 1
            RichTextBox1.SelStart = j
            RichTextBox1.SelLength = 1
            Colours(j) = RichTextBox1.SelColor
        Next j
    End If
    RichTextBox1.text = Left$(RichTextBox1.text, 25)
    If l > 0 Then
        For j = 0 To l - 1
            RichTextBox1.SelStart = j
            RichTextBox1.SelLength = 1
            RichTextBox1.SelColor = Colours(j)
        Next j
    End If
End Sub



Private Sub Form_Load()
Dim i As Integer
Dim j As Integer
Dim l As Integer
Dim ss As Integer
Dim Colours() As Long
Const strText = "Quick brown" & vbCrLf
RichTextBox1.text = ""
For i = 0 To 5
    l = Len(RichTextBox1.text)
    If l > 0 Then
        ReDim Colours(l - 1)
        For j = 0 To l - 1
            RichTextBox1.SelStart = j
            RichTextBox1.SelLength = 1
            Colours(j) = RichTextBox1.SelColor
        Next j
    End If
    RichTextBox1.text = RichTextBox1.text & strText
    RichTextBox1.SelStart = ss
    RichTextBox1.SelLength = Len(strText)
    Select Case i
        Case 0
        RichTextBox1.SelColor = vbRed
        Case 1
        RichTextBox1.SelColor = vbBlack
        Case 2
        RichTextBox1.SelColor = vbBlue
        Case 3
        RichTextBox1.SelColor = vbGreen
        Case 4
        RichTextBox1.SelColor = vbMagenta
        Case 5
        RichTextBox1.SelColor = vbYellow
    End Select
    ss = ss + Len(strText)
    If l > 0 Then
        For j = 0 To l - 1
            RichTextBox1.SelStart = j
            RichTextBox1.SelLength = 1
            RichTextBox1.SelColor = Colours(j)
        Next j
    End If

Next i

End Sub


ASKER CERTIFIED SOLUTION
Avatar of JR2003
JR2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just borrow jr2003 code and DON't ACCEPT THIS AS AN ANSWER

this is for a line


Private Sub rtfText_KeyUp(KeyCode As Integer, Shift As Integer)
    Dim iMaxWantedTextLine As Long, line As Long
    iMaxWantedTextLine = 5 'We don't want the text to get bigger than say 5 lines
   
   On Error Resume Next
   With rtfText
        line = UBound(Split(.Text, vbCrLf))
        If (line > iMaxWantedTextLine) Then
           'Delete text at the begining of the RichTextBox but preserve the formatting:
           .SelStart = 0
           .SelLength = InStr(1, .Text, vbCrLf) + 1
           .SelText = ""
           .SelStart = Len(.Text)
       End If
   End With

End Sub
you guys are great!, JR your code works very well,the text does jitter when alot of text comes in at once, but i will accept this answer and post another question to help fix the jitter in the code. thank you guys!