Finding+which+items+are+selected+in+a+List+View

//by Richard Russell, September 2010//

A significant difference between a **List View** and a **List Box** is that the former allows you to select **multiple** items whereas the latter only allows you to select a **single** item. This complicates somewhat the issue of discovering which item(s) is/are selected.

With a **List Box** you can simply send the **LB_GETCURSEL** message: code format="bb4w" SYS "SendMessage", hListBox%, LB_GETCURSEL, 0, 0 TO sel% code where **sel%** is set to the index of the current selection (or -1 if no item is selected).

With a **List View** you can optionally discover how many items are selected as follows: code format="bb4w" SYS "SendMessage", hListView%, LVM_GETSELECTEDCOUNT, 0, 0 TO nsel% code where **nsel%** is set to the number of selected items.

To find which item(s) is/are selected you can send the List View the **LVM_GETNEXTITEM** message: code format="bb4w" iStart% = -1 SYS "SendMessage", hListView%, LVM_GETNEXTITEM, iStart%, LVNI_SELECTED TO index% code where **index%** is set to the index of the first item selected (or to -1 if no items are selected).

To discover which other items (if any) are selected send the message repeatedly with **iStart%** set to the index previously returned, until **index%** is -1: code format="bb4w" iStart% = -1 REPEAT SYS "SendMessage", hListView%, LVM_GETNEXTITEM, iStart%, LVNI_SELECTED TO index% IF index% <> -1 THEN PROC_do_something(index%) iStart% = index% UNTIL index% = -1 code Use the **Windows Constants** utility or **API Viewer** to discover the values of the constants.