Hello, does anybody know how to count the number of lines that are in a multi-line textbox? Thank you.
Printable View
Hello, does anybody know how to count the number of lines that are in a multi-line textbox? Thank you.
Thanks for the link. I used the following API call. However this does not work with a TextBox you must use a Rich Edit Control for this to return the correct results.
Code:Private Declare Function SendMessageAsLong Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const EM_GETLINECOUNT = 186
Code:MsgBox SendMessageAsLong(MyRichEditBox.hWnd, EM_GETLINECOUNT, 0, 0)
You could also use
But for a lot of lines it might be a bit slow.Code:Dim Lns() As String
Lns() = Split(Text1.Text, vbNewLine)
MsgBox "There are " & UBound(Lns()) + 1 & " lines in textbox 1"
EM_GETLINECOUNT works fine with a standard TextBox
It doesn't work with a textbox for me. I have some text that is 22 lines long, in a textbox it returns 7 using EM_GETLINECOUNT, in a rich edit box it returns the correct 22.
works fine for me
I think it may have to do with how each control does word-wrapping. Try adding a Rich Edit box to your form and increase the amount of characters in your Text2 like this and the results will not be the same between the two controls:
Code:Text2.Text = String(22 * 22, "A")
RichTextBox1.Text = String(22 * 22, "A")
still not getting any problems (see attached) - you might get 1 or 2 lines difference do to character spacing (assuming the controls are the same width) - but EM_GETLINECOUNT still returns the correct number of lines.