• Welcome to Valhalla Legends Archive.
 

Deleting lines from RichText/RichEdit

Started by Grok, May 28, 2004, 10:29 AM

Previous topic - Next topic

Grok

This code is supposed to make sure I never go over 2000 lines in my RichTextBox.  It tries to delete any lines (from the top down) that exceed mMaxLines (2000). SOLVED.  I was using EM_SETSEL wrong.  The following code works.


Private Type TYPE_FINDTEXT
   cpMin As Long
   cpMax As Long
   lpstrText As String
End Type

Private Sub AddChat(ParamArray saElements() As Variant)
   Dim i As Integer
   For i = LBound(saElements) To UBound(saElements) Step 2
       With rtb
           .SelStart = Len(.Text) + 1
           .SelLength = 0
           .SelColor = saElements(i)
           .SelText = saElements(i + 1)            ' & Left$(vbCrLf, -2 * CLng((i + 1) = UBound(saElements)))
           .SelStart = Len(.Text) + 1
           .SelLength = 0
       End With
   Next i
   'keep only mMaxLines in the richtextbox
   Dim lRet As Long, LineCount As Long, TopLine As Long
   Dim strReplace As String, pCharPos As Long
   Dim lpFINDTEXTEX As TYPE_FINDTEXT
   strReplace = ""
   lpFINDTEXTEX.cpMin = 0                      'includes everything
   lpFINDTEXTEX.cpMax = -1                     'includes everything
   lpFINDTEXTEX.lpstrText = vbCrLf
   LineCount = SendMessage(rtb.hWnd, EM_GETLINECOUNT, ByVal 0&, ByVal 0&)
   Do While LineCount > mMaxLines
       pCharPos = SendMessage(rtb.hWnd, EM_FINDTEXTEX, ByVal 0&, lpFINDTEXTEX)
       If pCharPos > 0 Then
           pCharPos = pCharPos + 2
           lRet = SendMessage(rtb.hWnd, EM_SETSEL, ByVal 0&, ByVal CLng(pCharPos))
           lRet = SendMessage(rtb.hWnd, EM_REPLACESEL, ByVal 0&, ByVal strReplace)
       End If
       LineCount = SendMessage(rtb.hWnd, EM_GETLINECOUNT, ByVal 0&, ByVal 0&)
   Loop
   TopLine = SendMessage(rtb.hWnd, EM_GETFIRSTVISIBLELINE, ByVal 0&, ByVal 0&)
   frmGate.lblLineCount.Caption = LineCount
End Sub

Eric

#1
This seems a little easier:


If SendMessageA(RTB.hWnd, EM_GETLINECOUNT, &H0, &H0) >= 2000 Then
       With RTB
           .SelStart = 0
           .SelLength = SendMessageA(RTB.hWnd, EM_LINELENGTH, &H0, &H0) + 2
           .SelText = ""
       End With
End If

Grok

Good computer science.  My art sucked.

Skywing

Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.

Eli_1

I never found out how to make it stop doing that when your scrolled up -- how would you?  :o

Grok

Quote from: Skywing on May 30, 2004, 06:16 PM
Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.

Then explain how to delete lines without using EM_SETSEL or equivalent?

Eibro

Quote from: Grok on May 30, 2004, 07:58 PM
Quote from: Skywing on May 30, 2004, 06:16 PM
Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.

Then explain how to delete lines without using EM_SETSEL or equivalent?
Perhaps you could save the current selection and restore it afterwards.
Eibro of Yeti Lovers.

iago

Quote from: Eibro[yL] on May 30, 2004, 10:54 PM
Quote from: Grok on May 30, 2004, 07:58 PM
Quote from: Skywing on May 30, 2004, 06:16 PM
Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.

Then explain how to delete lines without using EM_SETSEL or equivalent?
Perhaps you could save the current selection and restore it afterwards.

But when you do that, don't forget that the location of the selection will change since you're removing text.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*