devxlogo

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

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 LongConst 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

See also  5 Strategies for Supercharging Your CRM
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist