LB_GETSELITEMS Message

Platforms

Description & Usage

Sending the LB_GETSELITEMS message to a multiple-selection list box retrieves an list of all the selected items. The index of each selected item is copied into an array passed with the message. For single-selection list boxes, send the LB_GETCURSEL message instead.

Return Value

If successful, the message returns the number of item indexes copied into the array. If an error occured, the function returns LB_ERR.

Visual Basic-Specific Issues

None.

Parameters

wParam
The number of list box item indexes that the array passed as lParam can hold.
lParam
The array that receives the indexes of the selected items. If the array is too small, then only some of the indexes are copied over. To get the minimum length needed for all the indexes, use the LB_GETSELCOUNT message.

Constant Definitions

Const LB_GETSELITEMS = &H191
Const LB_ERR = -1

Example

Display a list of all items that are selected in a list box. To use this example, place a multi-select 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_GETSELCOUNT = &H190
Public Const LB_GETSELITEMS = &H191

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

Private Sub Command1_Click()
	Dim items() As Long  ' indexes of the selected items
	Dim numsel As Long   ' number of selected items
	Dim c As Long        ' counter variable
	Dim retval As Long   ' return value
	
	' Count the number of selected items.
	numsel = SendMessage(List1.hWnd, LB_GETSELCOUNT, ByVal CLng(0), ByVal CLng(0))
	If numsel = 0 Then
		Debug.Print "No items are selected."
	Else
		' Resize the array so it can hold all the indexes.
		ReDim items(0 To numsel - 1) As Long
		' Get the indexes of all selected items.
		retval = SendMessage(List1.hWnd, LB_GETSELITEMS, ByVal numsel, items(0))
		' Display them.
		Debug.Print "The following items are selected (identified by index):"
		For c = 0 To numsel - 1
			Debug.Print items(c);
		Next c
		Debug.Print
	End If
End Sub

See Also

LB_GETCURSEL, LB_GETSEL, LB_GETSELCOUNT

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