Results 1 to 9 of 9

Thread: [RESOLVED] Listview Remove Item when unchecked

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Listview Remove Item when unchecked

    Hi,

    Ho do I remove an item from the listview when I gets unchecked? I can't use the SelectedItem, 'cause that isn't always the case. Just the Checked=False.

    Thanks for the reply in advance.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Listview Remove Item when unchecked

    Loop through all the items and if an item has unchecked, remove that item and reload the the list.

    Code:
    Private Sub Command1_Click()
    For n = 1 To lv.ListItems.Count
       If lv.ListItems(n).Checked = False Then
       'note the (n) item
    Next n
    'Reload every thing with out (n) item
    End Sub
    Last edited by Fazi; Oct 22nd, 2007 at 02:09 AM.

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Listview Remove Item when unchecked

    hmmm... Tried that, but I'm doing something wong there...

    Code:
    Private Sub ListView4_ItemCheck(ByVal Item As MSComctlLib.ListItem)
        For i = 1 To ListView4.ListItems.Count
        If Item(i).Checked = False Then
            ListView4.ListItems.Remove (i)
        End If
    End Sub


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Listview Remove Item when unchecked

    When iterating for deletion using For loops, do so in reverse order; from Count to 1. This is because the upper bound supplied to the For loop construct becomes obsolete once an item is removed.

  5. #5
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Listview Remove Item when unchecked

    Leinad31, what about my example. will that method ok?

  6. #6
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Listview Remove Item when unchecked

    Yes, but if the number of items to be removed is small then its easier and faster to go ahead with the removal thru reverse iteration (less intermediate data structure maintenance)

  7. #7

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Listview Remove Item when unchecked

    Example?


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  8. #8
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Listview Remove Item when unchecked

    Radjes, what leinad telling is ...

    Code:
    Private Sub Command1_Click()
    For n = lv.ListItems.Count to 1 step -1
       If lv.ListItems(n).Checked = False Then
          lv.ListItems.remove(n)
       End if
    Next n
    End Sub
    Last edited by Fazi; Oct 22nd, 2007 at 02:57 AM.

  9. #9

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Listview Remove Item when unchecked

    Seems that I was on the right track, but not there. Thank you both for the solution. Easier then expected.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

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