Re: Listview ( remove item ) in vb.net?

piojunbabia

Newcomer
Joined
Feb 15, 2011
Messages
1
Re: Listview ( remove item ) in vb.net?

split from http://www.xtremedotnettalk.com/showthread.php?t=42557, please don't reply to posts more than 30 days old, consider starting a new thread if it is a new problem. PD


ListView.Items.Remove removes a ListItem from the list by
passing that item to the method, so you can use a For Each loop
to loop through all the items and remove the offending ones.

Here lvw is a ListView, sName is the text of the item you want to
delete, and li is the ListItem class to loop with.

Visual Basic:
    Dim li As ListViewItem
    Dim sName As String

    For Each li In lvw.Items
      If li.Text = sName Then
        lvw.Items.Remove(li)
        ' If you only want to remove one item with that Text 
        ' you can put an Exit For right here
      End If
    Next

Hi, is there another way to delete an item in listview similar to the code you gave, but this time deleting a selected item like:

Visual Basic:
Dim li As ListViewItem
    Dim sName As String

    For Each li In lvw.Items
      If [U]li.selectedItem.Index[/U] Then
        lvw.Items.Remove(li)
        ' If you only want to remove one item with that Text 
        ' you can put an Exit For right here
      End If
    Next
the underlined code is the one i want to get.
 
Last edited by a moderator:
Re: Listview ( remove item ) in vb.net?

Not entirely sure what your question is - are you trying to get the index or the item the index refers to? If you are after the index then what problem are you having?
 
Back
Top