Solved

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

Posted on 2004-08-23
192 Views
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
Question by:andyakira
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2
• +4

LVL 12

Expert Comment

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

ID: 11877503
Thanks bob, this works.  but its deleting all my colors in the RTB.
0

LVL 12

Expert Comment

ID: 11877523
andyakira,

what colors?

Bob
0

Author Comment

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

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

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

ID: 11877682
:D thanks
0

LVL 19

Expert Comment

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

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

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

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

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

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

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

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

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

Question has a verified solution.

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

### Suggested Solutions

using Access 8 95
VB 6 error 5 in windows 10 but not in XP 7 72
which modules are active in VB6 project? 6 57
Remove Exif Orientation before adding image to db 9 63
Iâ€™ve seen a number of people looking for examples of how to access web services from VB6.  Iâ€™ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web servâ€¦
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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â€¦
###### Suggested Courses
Course of the Month3 days, 10 hours left to enroll