Retrieving the currently selected item(s) from a list box


To retrieve the currently selected item from a list box use the Index property. For example, the following could be used to return the index number of the currently selected item:

 CurSelectedItem = .listControl1.Index

To return the currently selected string the Index property is used in conjunction with the GetString$() method, for example:

 Selected$ = .listControl1.GetString$(.listControl1.Index)

If the list box is a multiple or extended selection list box, as set by the Selection property, then the index numbers of the selected items can be returned with the GetSelection() method. The specified value corresponds to the ordinal number of the selected item, i.e. passing a value of 1 would return the index of the first selected item in the list, for example:

 SelectedItem1 = .listControl1.GetSelection(1)

Note that if the specified item is not selected then the GetSelection() method returns a value of -1.

To return the text of selected items in a multiple or extended selection list box the GetSelection() method can be used in conjunction with the GetString() method, for example:

 Selected$ = .listControl1.GetString$(.listControl1.GetSelection(Number))

There is also an object notation for iterating over a multi-selection using the SelectedFirst and SelectedNext object properties.