Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-08-23
14
Medium Priority
?
197 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:Bob Lamberson
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:Bob Lamberson
ID: 11877523
andyakira,

what colors?

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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:Bob Lamberson
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 77

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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

572 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