LB_GETCOUNT Message

Platforms

Description & Usage

Sending the LB_GETCOUNT message determines how many items exist in a list box. Keep in mind that the list box items are zero-based; the first item has an index of zero, and the last one has an index of the count minus one.

Return Value

If successful, the message returns the number of items that are in the list box control. If an error occured, the message returns -1.

Visual Basic-Specific Issues

None.

Parameters

wParam
Not used -- set to 0.
lParam
Not used -- set to 0.

Constant Definitions

Const LB_GETCOUNT = &H18B

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

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_getcount.html