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

Recommended Answers

All 3 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.