View Full Version : Solved: List box items selected but wrong message

05-28-2008, 05:49 AM
In the attached file I have a list box. On initializing of list box it is adding itmes to the list box. Now if "Select All" button is clicked, it selects all the items in the list box then if "Print" button is clicked it shows "No items selected."

But after initializing if you manually select an item and then click the "Select All" button then the "Print " button shows correct message.

Why is this happening?

05-28-2008, 06:09 AM
The .List of a List box is a 0 based array. If the first item on the list is selected, then .ListIndex = 0. If nothing is selected, .ListIndex = -1
Changing the test in the routine should set things right.
Private Sub CommandButton4_Click()
Dim i As Long
If ListBox1.ListIndex = -1 Then
MsgBox "No items selected!"
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
MsgBox "Selected Item: " & ListBox1.List(i)
End If
Next i
End If
End Sub

05-28-2008, 06:22 AM
Thanks its working correctly now.