Hi,
Can anyone provide an example how to use .Selected property on a listbox to check which itmes are currently checked? As I mentioned in the subject, it's a listbox with Style set to 1-checkbox.
Thanks
Printable View
Hi,
Can anyone provide an example how to use .Selected property on a listbox to check which itmes are currently checked? As I mentioned in the subject, it's a listbox with Style set to 1-checkbox.
Thanks
Code:'Add 2 items to a listbox:
List1.AddItem "String1", 0
List1.AddItem "String2", 1
'Remove the 1 item:
List1.RemoveItem 0
'Verify which items is checked:
If List1.Selected(0) = True Then
MsgBox "String2 is selected"
End If
'Note: If you remove 1 item, the index numbers
'of all other items will change!
'e.g. If you remove item(0) just like in this
'example, the item which was named "String2"
'gets the index 0.
'Thats why I typed "String2" in the messagebox statement.
Add to a Form with a ListBox and CommandButton
Code:Private Sub Command1_Click()
'Loop through and check if they are selected
For I = 0 To List1.ListCount - 1
If List1.Selected(I) = True Then MsgBox (List1.List(I) & " is selected")
Next I
End Sub
Private Sub Form_Load()
'Add items to ListBox
For I = 0 To 10
List1.AddItem "Number" & I
Next I
End Sub