Article ID: 110957
Article Last Modified on 10/29/2003
[form.]{filelistbox|listbox}.Selected(index)[ = {True|False}]
The Selected property settings are:
True = The item is selected. False = (Default) The item is not selected.The Selected property is particularly useful where users can make multiple selections. You can quickly check which items in a list are selected. Your code can use this property to select or deselect items in a list.
Sub Form_Load ()
For i = 1 To 5
list1.AddItem Str$(i) 'Add more than 2 items to the list box.
Next
End Sub
Sub List1_Click ()
Static x ' Preserve the value of x between Click events.
x = x + 1 ' Increment the count of Click events.
Print x ' Print the cumulative number of click events.
' The following statement only causes a second event when an item
' other than the first item is clicked. Clicking the first item
' (item 0) does not cause a second event, because list1.Selected(0)
' is already True:
list1.Selected(0) = True ' Selects the first item.
End Sub
Sub File1_Click ()
' If no item is selected in the file list box, exit the sub:
If File1.ListIndex = -1 Then Exit Sub
File1.Selected(File1.ListIndex) = False
End Sub
NOTE: If you left out the above If statement, then the
File1.Selected(File1.ListIndex) statement would give the following
error when you click the file list box:
The index value of the File1.Selected() property must be greater
than 0. By default, File1.ListIndex starts as -1.
Sub File1_Click ()
' NOTE: If you delete the following line, you get "Out of Stack
' Space" due to recursion (about 30 iterations):
If File1.ListIndex = -1 Then Exit Sub
File1.Selected(0) = True
File1.Selected(0) = False 'Resets first item to nonselected.
End Sub
Additional query words: 3.00
Keywords: KB110957