Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Grok on May 28, 2004, 10:29 AM

Title: Deleting lines from RichText/RichEdit
Post by: Grok on May 28, 2004, 10:29 AM
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
Title: Re:Deleting lines from RichText/RichEdit
Post by: Eric on May 28, 2004, 01:27 PM
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
Title: Re:Deleting lines from RichText/RichEdit
Post by: Grok on May 28, 2004, 11:28 PM
Good computer science.  My art sucked.
Title: Re:Deleting lines from RichText/RichEdit
Post by: 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.
Title: Re:Deleting lines from RichText/RichEdit
Post by: Eli_1 on May 30, 2004, 07:35 PM
I never found out how to make it stop doing that when your scrolled up -- how would you?  :o
Title: Re:Deleting lines from RichText/RichEdit
Post by: 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?
Title: Re:Deleting lines from RichText/RichEdit
Post by: Eibro 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.
Title: Re:Deleting lines from RichText/RichEdit
Post by: iago on May 31, 2004, 12:41 PM
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.