Click to See Complete Forum and Search --> : Delete Checked Items in ListView


NV2002
March 2nd, 2003, 06:49 AM
Hi All ,

I have written a Subroutine to DELETE all of the checked Items in a ListView fixed in 'Details' Mode and has Checkboxes at all items.

' Routine to Remove Checked Items in a ListView
' Example Usage : RemoveCheckedItemsLV(Listview1)

Public Sub RemoveCheckedItemsLV(ByRef LV As ListView)
LV.BeginUpdate() ' Turn off the ListView
Do Until LV.CheckedIndices.Count = 0
Dim i As Integer
For i = 0 To LV.Items.Count - 1
If LV.Items(i).Checked = True Then
LV.Items.RemoveAt(i)
Exit For ' Fresh Start the Loop again INDEX CHANGED
End If
Next
Loop
LV.EndUpdate() ' Turn on the Listview
End Sub


When I have Hundreds of Items it seems too slow. Is there another way to delete Checked Items that is quicker.

Nick

DSJ
March 3rd, 2003, 08:37 AM
Go from then end of the items back to 0, that way their indexes don't change every time one is deleted... something like

For i = ListView.Items.Count to 0 step -1
if ListView.Items(i).Selected then
ListView.Items(i).remove
end if
next i

pgugel
March 3rd, 2003, 09:32 AM
Public Sub RemoveCheckedItems()
ListView1.BeginUpdate() ' Turn off the ListView
Dim I As Integer
For I = ListView1.Items.Count - 1 To 0 Step -1
If ListView1.Items(I).Checked = True Then
ListView1.Items.RemoveAt(I)
End If
Next I
ListView1.EndUpdate() ' Turn on the Listview
End Sub

NV2002
March 3rd, 2003, 08:45 PM
Thanks All who Replied :). It is such a simple solution and that is exactly why I missed it. Yes work from the END of the list not the start. I wish I had that thought. I will try the code that way.
Thanks Again :)