i have a (check style)list box which have some items.i want to remove more than one items which are selected(checked) .
this removing event is done by clicking on command button.
but it gives error of "invalid property array index"
plz help me to correct this error....

this my coding.....>>>

Private Sub cmdremove_Click()
For i = 0 To list1.ListCount - 1
If list1.Selected(i) = True Then
  list1.RemoveItem (i)
  End If
Next i
End Sub

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

try this

Private Sub cmdremove_Click()
         For i = 0 To list1.ListCount - 1
            If list1.Selected(i) = True Then
               list1.RemoveItem i 
            End If
         Next i
      End Sub

Edited 5 Years Ago by debasisdas: n/a

that error shows because you deleted,
if u have 5 items the loop starts i o to 4 and once you deleted an item then normally total items reduced to 3 but the loop only stops at 4, thats the error !
try something like this

Private Sub cmdremove_Click()
Dim j As Integer
        For i = 0 To List1.ListCount - 1
            If List1.Selected(j) = True Then
               List1.RemoveItem j
               j = j - 1
            End If
            j = j + 1
         Next i
End Sub

Or how about

Private Sub cmdremove_Click()
         For i = list1.ListCount - 1 to 0 step -1
            If list1.Selected(i) = True Then
               list1.RemoveItem i 
            End If
         Next i
      End Sub

Start at the end and move to the beginning.

Edited 5 Years Ago by WaltP: n/a

This question has already been answered. Start a new discussion instead.