Visual Basic Text API Routines
SendMessage: Find Text Box Document Size via API
     
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: Determine the Number of Lines in a Text Box
SendMessage: Text Selection Methods via API
SendMessage: Text Range Selection via API
     
 Prerequisites
None.

This demos using the API to retrieve the line count, line index and line length, to display the size of a document in a textbox..
 BAS Module Code
None.

 Form Code
Add the following code to the form. The demo shows calling the method from the textbox change event::

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
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1


Sub Text1_Change()

    Dim lineCount as Long
    Dim ChrsUpToLast As Long
    Dim DocumentSize As Long

    On Local Error Resume Next
   
   'first obtain the number of lines in the textbox
    lineCount = SendMessage(Text1.hwnd, _
                            EM_GETLINECOUNT, _
                            0&, _
                            ByVal 0&)

   
   'Next, determine the number of characters up to the 
   'last line in the textbox
    ChrsUpToLast = SendMessage(Text1.hwnd, _
                               EM_LINEINDEX, _
                               lineCount - 1, _
                               ByVal 0&)

    If ChrsUpToLast = 0 Then
    
        DocumentSize = 0
        
    ElseIf ChrsUpToLast < 65000 then
    
       DocumentSize = SendMessage(Text1.hwnd, _
                                  EM_LINELENGTH, _
                                  ChrsUpToLast, _
                                  ByVal 0&) + ChrsUpToLast
                                  
    End If
    
    Label1.Caption = DocumentSize

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.

With EM_LINEINDEX, the value of the wParam parameter specifies the zero-based line number. A value of -1 specifies the current line number (the line that contains the caret). lParam is not used an must be 0.

With EM_LINELENGTH, the value of the wParam parameter specifies the character index of a character in the line whose length is to be retrieved. If this parameter is -1, the message returns the number of unselected characters on lines containing selected characters. lParam is not used an must be 0.

The statement "ElseIf ChrsUpToLast <65000 Then" is for 32-bit VB .. vb4-16 and vb3 should use no more than 32000.

 
 

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