We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 40,346 times

Related Categories

Counting lines in a multi-line textbox

Often, you may need to know how many lines of text there are in a multi-line textbox. While Visual Basic makes it easy to determine how many paragraphs the textbox contains, using code like so:

Private Sub Command1_Click()
    Dim myParas As Variant
    myParas = Split(Text1, vbNewLine)
    MsgBox UBound(myParas) + 1
End Sub


This code, as you can see, only parses out hard carriage returns; whereas you want to know how many lines of text the control contains.

To accomplish this task, you'll need to resort to the SendMessageAsLong() API function. This function conforms to the following syntax:

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


To obtain the number of lines, you pass in the EM_GETLINECOUNT constant in the wMsg parameter. Declare this constant like this:

Const EM_GETLINECOUNT = 186

Finally, to use the function, pass in the handle to the textbox from which you want to retrieve the line count, as well as the wMsg we mentioned earlier. Example code might look like so:

Private Sub Command2_Click()
    Dim lCount As Long
    lCount = SendMessageAsLong(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
    MsgBox lCount
End Sub

© 2001 Element K Journals, a division of Element K Press LLC ("Element K"). Element K and the Element K logo are trademarks of Element K LLC

Comments

  • Re: [1607] Counting lines in a multi-line textbox

    Posted by kreylor on 28 Dec 2007

    Just a curious thought.


    Why would you go through that much trouble to count lines in a multi-line textbox when all you have to do is:

  • Re:

    Posted by kreylor on 28 Dec 2007

    [quote user="vsnlth"]Hi

    If I wanted to display each line in a multiline textbox, intead of the numbers, how would I get msgbox, to display the content[/quote]


  • Posted by vsnlth on 24 Nov 2005

    [quote][1]Posted by [b]MathiasRav[/b] on 7 Mar 2004 11:25 AM[/1]
    I think I misunderstood you, but if you want to display the content of a multiline textbox, use this code:
    [code]MsgBox Text1.Text, ,...

  • Posted by vsnlth on 24 Nov 2005

    [quote][1]Posted by [b]MathiasRav[/b] on 7 Mar 2004 11:25 AM[/1]
    I think I misunderstood you, but if you want to display the content of a multiline textbox, use this code:
    [code]MsgBox Text1.Text, ,...

  • hi

    Posted by vsnlth on 24 Nov 2005

    Hi

    If I wanted to display each line in a multiline textbox, intead of the numbers, how would I get msgbox, to display the content