LB_GETCURSEL Message

Platforms

Description & Usage

Sending the LB_GETCURSEL message to a single-selection list box gets the index of the selected item. To get the items selected in a multiple-selection list box, send the LB_GETSELITEMS message instead.

Return Value

If successful, the message returns the zero-based index of the selected item. If no item is selected, or if an error occured, the message returns LB_ERR.

Visual Basic-Specific Issues

None.

Parameters

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

Constant Definitions

Const LB_GETCURSEL = &H188
Const LB_ERR = -1

Example

Print the text of the selected item in single-selection list box List1. To use this example, 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_GETCURSEL = &H188
Public Const LB_GETTEXT = &H189
Public Const LB_GETTEXTLEN = &H18A
Public Const LB_ERR = -1

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

Private Sub Command1_Click()
	Dim selitem As Long     ' index of the selected item
	Dim textlen As Long     ' length of the item's text
	Dim itemtext As String  ' text of the selected item
	
	' Get the index of the selected item.
	selitem = SendMessage(List1.hWnd, LB_GETCURSEL, ByVal CLng(0), ByVal CLng(0))
	If selitem = LB_ERR Then
		Debug.Print "No item is selected."
	Else
		' Get the text of the selected item.
		textlen = SendMessage(List1.hWnd, LB_GETTEXTLEN, ByVal selitem, ByVal CLng(0)) + 1
		itemtext = Space(textlen)
		textlen = SendMessage(List1.hWnd, LB_GETTEXT, ByVal selitem, ByVal itemtext)
		' Print the result.
		Debug.Print "Selected item: "; Left(itemtext, textlen)
	End If
End Sub

See Also

LB_GETSELITEMS, LB_SETCURSEL

Category

List Boxes

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


Last Modified: January 21, 2001
This page is copyright © 2001 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_getcursel.html