Determining Number of Lines in VB Text Box; SendMessage API

ID Number: Q72719

1.00

WINDOWS

Summary:

To determine the number of lines of text within a text box control,

call the Windows API function SendMessage with EM_GETLINECOUNT(&H40A)

as the wMsg argument.

Calling SendMessage with the following parameters will return the

amount of lines of text within a text box:

hWd% - Handle to the text box.

wMsg% - EM_GETLINECOUNT(&H40A)

wParam% - 0

lParam% - 0

This information applies to Microsoft Visual Basic programming

system version 1.0 for Windows.

More information:

As an example to determine the amount of lines within a text box,

perform the following:

1. Create a form with a text box and a command button. Change the

MultiLine property of the text box to TRUE.

2. Declare the API SendMessage function in the global-declarations

section of your code window. (the Declare statement must be

on just one line):

Declare Function SendMessage% Lib "user" (ByVal hWd%,

ByVal wMsg%,

ByVal wParam%,

ByVal lParam&)

3. You will need to declare another API routine to get the handle of

the text box. Declare this also in your global-declarations section

of your code window. The returned value will become the hWd%

argument to the SendMessage function.

Declare Function GetFocus% Lib "user" ()

4. Within the click event of your button, add the following code:

Sub Command1_Click ()

Const EM_GETLINECOUNT = &H40A 'defined within SDK WINDOWS.H

' command button has focus, give focus to text box.

Text1.SetFocus

' get the handle of the text box.

hWd% = GetFocus()

' print the amount of lines to the immediate window.

Debug.Print SendMessage(hWd%, EM_GETLINECOUNT, 0, 0)

End Sub

5. Run the program. Add several lines of text to the text box. Click

the command button to see the number of lines printed out to the

immediate window.

References:

"Programming Windows: the Microsoft Guide to Writing Applications for

Windows 3," by Charles Petzold (published by Microsoft Press, 1990)

"Peter Norton's Windows 3.0 Power Programming Techniques," by Peter

Norton and Paul Yao (published by Bantam Computer Books, 1990)

"Microsoft Windows 3.0 Software Development Kit: Reference Volume 1"

The WINSDK.HLP file shipped with Microsoft Windows 3.0 Software

Development Kit

Additional reference words: 1.00 3.00