Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Mar 31, 2001

TextBoxGetLines - Get an array with individual lines in a TextBox control

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
Const EM_FMTLINES = &HC8

' Return an array with all the lines in the control.
' If the second optional argument is True, the hard CR-LFs are preserved
' (hard CR-LFs are those inserted by the user, soft CRLFs are those inserted
'  automatically to wrap long lines)

Function TextBoxGetLines(tb As TextBox, Optional KeepHardLineBreaks As Boolean) _
    As String()
    Dim result() As String
    Dim i As Long

    ' Activate soft line breaks. A soft line break is marked by the
    ' CR-Cr-LF sequence.
    SendMessage tb.hwnd, EM_FMTLINES, True, ByVal 0&

    ' Retrieve all the lines in one operation and split results. 
    ' This operation will leave trailing CR character for soft line breaks only.
    result() = Split(tb.Text, vbCrLf)

    ' We need a loop to trim the trailing CR character. If the second
    ' argument is true, we need to manually add a CR-LF pair to all
    ' the lines that don't contain such trailing CR char.
    For i = 0 To UBound(result)
        If Right$(result(i), 1) = vbCr Then
            result(i) = Left$(result(i), Len(result(i)) - 1)
        ElseIf KeepHardLineBreaks Then
            result(i) = result(i) & vbCrLf
        End If
    Next

    ' Deactivate soft line breaks.
    SendMessage tb.hwnd, EM_FMTLINES, False, ByVal 0&

    TextBoxGetLines = result()
End Function
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap