Solved

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

Posted on 2004-08-23
14
190 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
ID: 11877272
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
ID: 11877503
Thanks bob, this works.  but its deleting all my colors in the RTB.
0
 
LVL 12

Expert Comment

by:BobLamberson
ID: 11877523
andyakira,

what colors?

Bob
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:andyakira
ID: 11877540
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
ID: 11877620
>> 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
ID: 11877640
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
ID: 11877682
:D thanks
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 11877776
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
ID: 11877827
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
ID: 11878066
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
ID: 11879223
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
ID: 11879757
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
ID: 11880718
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
ID: 11887726
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

828 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