
Getting each line from a text box (or RichTextBox)
Filip De Vos has already posted an answer which will enable you to get
complete "lines" (ie ending in a CrLf). Most people (and all standard word
processors) refer to these as paragraphs and so his code will get individual
paragraphs for you. If this is what you want then you can completely ignore
the rest of this message :-(
:-) You may, however, want to get lines exactly as they appear in the Text
Box. If this is what you want then the following stuff (which I got some
time ago from this Newsgroup, should do it for you.
Mike
Put the following in the General Declarations section of a form:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_GETLINE = &HC4
Use this code to get the lines:
Dim lLines As Long, lRet As Long
Dim i As Integer
Dim sBuffer As String * 80
'Make sure there's something in the box
If Len(Text1) Then
'Get the number of lines in the box
lLines = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
'Set up the buffer to receive the text
MsgBox "Number of Lines: " & lLines
Mid$(sBuffer, 1, 1) = Chr$(79 And &HFF)
Mid$(sBuffer, 2, 1) = Chr$(79 \ &H100)
'Retrieve each line into the buffer & print
For i = 0 To lLines - 1
lRet = SendMessage(Text1.hwnd, EM_GETLINE, i, ByVal sBuffer)
MsgBox Left$(sBuffer, lRet)
Next
End If
The code assumes there is no more than 79 characters in each line. If you
have more, you need to increase the size of sBuffer and the numbers (79)
used
in setting up the buffer.
Quote:
> Using VB5, can anyone please show me how to get each line from a
> multi-line text box or RichTextBox? So that I could have each line
> stored in an array.
> Regards, Otser.