Simon Canning
Simon Canning

Reputation: 215

Remove items from listview

I have a listview that has multiple entries and each entry has 2 subitems. I am wanting to know how to remove each item in the listview where the subitem(1) equals a certain string.

What would be the best way to do this?

thanks

Upvotes: 2

Views: 34354

Answers (5)

Ameer Chand
Ameer Chand

Reputation: 51

Dim x As Integer = 0

    For Each item6 As ListViewItem In ListView4.Items

        Dim f As String = item6.SubItems(1).Text
        Dim ind As Integer = item6.Index
        For Each item7 As ListViewItem In ListView4.Items
            Dim f2 As String = item7.SubItems(1).Text
            ' MsgBox(f & " 2nd value " & f2)
            If (f = f2) Then
                x = x + 1
                ' MsgBox(x & "= time matched" & f)
                If (x > 1) Then
                    MsgBox("delete here")
                    ListView4.Items.Remove(item6)

                End If
            End If
        Next
        x = 0
    Next

Upvotes: -1

user2074102
user2074102

Reputation:

This is probably the easiest way of removing all the list items.

Do While YOURITEMLIST.Items.Count <> 0
    YOURITEMLIST.Items.Remove(YOURITEMLIST.Items(0))
Loop

Upvotes: 0

Robert Beaubien
Robert Beaubien

Reputation: 3156

You can't use a for..each loop to remove items. after you remove the first item, the for...each is broken.

Try this:

        Dim pos As Int32
    Dim listItem As ListViewItem

    For pos = lvw.Items.Count - 1 To 0 Step -1
        listItem = lvw.Items(pos)
        If listItem.SubItems(1).Text = "testvalue" Then
            lvw.Items.Remove(listItem)
        End If
    Next

Upvotes: 6

Mark Hall
Mark Hall

Reputation: 54562

You can try something like this.

For Each listItem As ListViewItem In ListView1.Items
    If listItem.SubItems.Item(1).Text = "SomeName" Then
        listItem.Remove()
    End If
Next

Upvotes: 1

Dennis
Dennis

Reputation: 4017

Dim listItem As ListViewItem
    Dim someName As String

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

Upvotes: 3

Related Questions