LB_GETTEXTLEN Message

Platforms

Description & Usage

The LB_GETTEXTLEN message retrieves the length of the text of one of the items in a list box control. The reported length does not include its terminating null character. When determining how long to size a string to receive the list box item's text, remember to add one to include the null.

Return Value

If successful, the message returns the length of the list box item's text, not including the terminating null character. If an error occured, the message returns -1.

Visual Basic-Specific Issues

None.

Parameters

wParam
The zero-based index of the list box item to retrieve the length of the text of. (The first item has an index of 0, etc.)
lParam
Not used -- set to 0.

Constant Definitions

Const LB_GETTEXTLEN = &H18A

Example

Display the text of the second-to-last item in list box List1 when the user clicks button Command1. Obviously, to use this example you must place a list box named List1 and a command button named Command1 on a form window.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd _
	As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Public Const LB_GETCOUNT = &H18B
Public Const LB_GETTEXT = &H189
Public Const LB_GETTEXTLEN = &H18A

' *** Place the following code inside a form window. ***

Private Sub Command1_Click()
	Dim count As Long       ' number of items in the list box
	Dim s2l As Long         ' index of the second-to-last item
	Dim itemtext As String  ' text of that item
	Dim textlen As Long     ' length of the item text
	
	' Figure out the index of the second-to-last item by subtracting two from
	' the total item count (remember, the index is zero-based).
	count = SendMessage(List1.hWnd, LB_GETCOUNT, ByVal CLng(0), ByVal CLng(0))
	s2l = count - 2
	
	' Make the string long enough to receive that item's text.
	textlen = SendMessage(List1.hWnd, LB_GETTEXTLEN, ByVal s2l, ByVal CLng(0))
	itemtext = Space(textlen) & vbNullChar
	' Get the item text and remove the trailing null.
	textlen = SendMessage(List1.hWnd, LB_GETTEXT, ByVal s2l, ByVal itemtext)
	itemtext = Left(itemtext, textlen)
	' Finally, display the result.
	Debug.Print "The second-to-last item is "; itemtext
End Sub

See Also

LB_GETTEXT

Category

List Boxes

Back to the Message list.
Back to the Reference section.


Last Modified: December 17, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/l/lb_gettextlen.html