Solved

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

Posted on 2004-08-23
14
187 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:andyakira
  • 5
  • 3
  • 2
  • +4
14 Comments
 
LVL 12

Expert Comment

by:BobLamberson
Comment Utility
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
0
 

Author Comment

by:andyakira
Comment Utility
Thanks bob, this works.  but its deleting all my colors in the RTB.
0
 
LVL 12

Expert Comment

by:BobLamberson
Comment Utility
andyakira,

what colors?

Bob
0
 

Author Comment

by:andyakira
Comment Utility
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


0
 
LVL 7

Expert Comment

by:Burbble
Comment Utility
>> 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
0
 
LVL 7

Expert Comment

by:Burbble
Comment Utility
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
0
 

Author Comment

by:andyakira
Comment Utility
:D thanks
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
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


0
 
LVL 12

Expert Comment

by:BobLamberson
Comment Utility
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
0
 

Author Comment

by:andyakira
Comment Utility
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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


0
 
LVL 18

Accepted Solution

by:
JR2003 earned 500 total points
Comment Utility
Private Sub Command1_Click()

    Dim iMaxWantedTextLength As Long
    iMaxWantedTextLength = 500 'We don't want the text to get bigger than say 500 characters
   
    With RichTextBox1
        If Len(.Text) > iMaxWantedTextLength Then
            'Delete text at the begining of the RichTextBox but preserve the formatting:
            .SelStart = 0
            .SelLength = Len(.Text) - iMaxWantedTextLength
            .SelText = ""
        End If
    End With

End Sub
0
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
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
0
 

Author Comment

by:andyakira
Comment Utility
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!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now