CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2002
    Location
    USA
    Posts
    207

    Delete Checked Items in ListView

    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

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    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

  3. #3
    Join Date
    Aug 2001
    Location
    Rochester, NY
    Posts
    28

    this also worked

    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

  4. #4
    Join Date
    Dec 2002
    Location
    USA
    Posts
    207

    Smile

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured