Valhalla Legends Forums Archive | Visual Basic Programming | Deleting lines from RichText/RichEdit

AuthorMessageTime
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.

[code]
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[/code]
May 28, 2004, 3:29 PM
LoRd
This seems a little easier:

[code]
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
[/code]
May 28, 2004, 6:27 PM
Grok
Good computer science. My art sucked.
May 29, 2004, 4:28 AM
Skywing
Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.
May 30, 2004, 11:16 PM
Eli_1
I never found out how to make it stop doing that when your scrolled up -- how would you? :o
May 31, 2004, 12:35 AM
Grok
[quote author=Skywing link=board=31;threadid=7000;start=0#msg62847 date=1085958990]
Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.
[/quote]

Then explain how to delete lines without using EM_SETSEL or equivalent?
May 31, 2004, 12:58 AM
Eibro
[quote author=Grok link=board=31;threadid=7000;start=0#msg62864 date=1085965121]
[quote author=Skywing link=board=31;threadid=7000;start=0#msg62847 date=1085958990]
Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.
[/quote]

Then explain how to delete lines without using EM_SETSEL or equivalent?
[/quote]Perhaps you could save the current selection and restore it afterwards.
May 31, 2004, 3:54 AM
iago
[quote author=Eibro[yL] link=board=31;threadid=7000;start=0#msg62891 date=1085975676]
[quote author=Grok link=board=31;threadid=7000;start=0#msg62864 date=1085965121]
[quote author=Skywing link=board=31;threadid=7000;start=0#msg62847 date=1085958990]
Note that this will cause major annoyance if you are trying to select some text while scrolled up a bit.
[/quote]

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

But when you do that, don't forget that the location of the selection will change since you're removing text.
May 31, 2004, 5:41 PM

Search