How to count number of lines in a Text box
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 | 'Controls - How to count number of lines in a Text box Option Explicit Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, _ ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long Public Function NumberOfLines(txt As TextBox) Const EM_GETLINECOUNT As Long = &HBA NumberOfLines = SendMessage(txt.hWnd, EM_GETLINECOUNT, 0, ByVal 0) End Function |
April 21st, 2009 - 05:51
I’m not fluent in VB. But I tried this and it worked:
‘ 1. Get number of characters in Text Box
‘ 2. Find “line number” of last character
‘ 3. Display line number in label
Label1.Text = TextBox1.GetLineFromCharIndex(TextBox1.TextLength).ToString
Visual Studio 2008 on standard dialog form in a Visual Basic project/solution.
This is the _TextChanged event for TextBox1 (auto created by the interface when I invoked the TextChanged event) in MainForm.vb