I've recently wrote a routine to replace visual line breaks by real ones in a RichTextBox. It works like intended, but is extremely slow. Would someone know how could it be accelerated?
Here's the code:
VB Code:
Public Sub InsertLineBreaks(rtb As RichTextBox) Dim xPos() As Long Dim lastLine As Long, curLine As Long Dim i As Long, l As Long Do rtb.Text = Replace$(rtb.Text, vbCrLf & vbCrLf, vbCrLf) Loop While InStr(1, rtb.Text, vbCrLf & vbCrLf) ReDim xPos(rtb.GetLineFromChar(Len(rtb.Text)) - 1) For i = 0 To Len(rtb.Text) curLine = rtb.GetLineFromChar(i) If curLine <> lastLine Then xPos(UBound(xPos)) = i + 1 l = l + 1 End If lastLine = rtb.GetLineFromChar(i) Next rtb.Text = Replace$(rtb.Text, vbCrLf, " ") If UBound(xPos) > 0 Then For i = 0 To UBound(xPos) rtb.SelStart = xPos(i) - (i * 2) + ((i - 1) * 2) rtb.SelLength = 1 If rtb.SelText <> " " Then rtb.SelStart = rtb.SelStart - 1: rtb.SelLength = 1 rtb.SelText = Chr(201) Next rtb.Text = Replace$(rtb.Text, Chr(201), vbCrLf) End If End Sub




Reply With Quote