Visual Basic Text API Routines
SendMessage: Determine the Number of Lines in a Text Box
     
Posted:   Saturday July 18, 1998
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6, and VB3, VB4-16 with appropriate declarations
Developed with:   VB5, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

SendMessage: Determine the Current Line in a Text Box
SendMessage: Find Text Box Document Size via API
SendMessage: Text Selection Methods via API
SendMessage: Text Range Selection via API
     
 Prerequisites
None.

This method is straightforward .. it uses SendMessage to retrieve the number of lines in a textbox.  A "line" for this method is defined as each new line after a word-wrap, in other words, each line showing against the left margin.  It does not mean, and is independent of, the actual number of lines delimited by hard returns in the text.
 BAS Module Code
None.

 Form Code
To the form, add a textbox (Text1), label (Label1), along with the following code:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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

Private Const EM_GETLINECOUNT = &HBA


Sub Text1_Change()

    Dim lineCount as Long
    On Local Error Resume Next
  
   'get/show the number of lines in the edit control
    lineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&)
    Label1.Caption = Format$(lineCount, "##,###")

End Sub
 Comments
The textbox passed to the SendMessage API must have its multiline property set to true at design time.

The EM_GETLINECOUNT message does not pass additional parameters to the API in the wParam or lParam variables. These must be 0.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

 

Hit Counter