Retrieving each line from a RichTextBox...

  • Thread starter Thread starter Mark Raishbrook
  • Start date Start date
M

Mark Raishbrook

....without resorting to SendMessage. Imagine, for example,
I have a small RTB whose text has wrapped as follows:

"This is a small test to see if I can get"
"both of these lines".

GetLineFromCharIndex(RTB.TextLength) returns 1, telling me
there are two lines in there. Using the Lines property to
retrieve them will fail, however. As the line has
soft-wrapped (i.e. the user has not hit Enter after "get"),
it returns "This is a small test to see if I can get both of
these lines".

In VB6, SendMessage would come to the rescue here. Is there
a managed way of achieving the same thing?
 
Not sure if this helps, but........

I used this in VBA, it might come in handy. It does exactly what you want.
You would just need to translate it. (sorry!)

Function GetLines() As Variant
Dim lLines As Long
With MyForm.RichText1
.SetFocus
lLines = .LineCount
End With

ReDim arrTmp(lLines) As String
For X = 1 To lLines
'Set and select linenumber
SetLine X
'pick up text in line
arrTmp(X) = n
Next
'return values to function
GetLines = arrTmp
Erase arrTmp

End Function

Sub SetLine(ByVal LineNumber As Integer)
On Error Resume Next
'set linenumber
MyForm.RichText1.CurLine = LineNumber - 1
'make selection using sendkeys (argghh)
SendKeys "{HOME}", -1
SendKeys "+{END}", -1
If Err <> 0 Then Err.Clear
End Sub
 
Thanks for that, Bob. I'd rather not use SendKeys, but if
all else fails I'll give it a go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top