vb.net 从列表视图中删除项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9139147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:55:13  来源:igfitidea点击:

Remove items from listview

vb.net

提问by Simon Canning

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.

我有一个包含多个条目的列表视图,每个条目有 2 个子项。我想知道如何删除列表视图中 subitem(1) 等于某个字符串的每个项目。

What would be the best way to do this?

什么是最好的方法来做到这一点?

thanks

谢谢

回答by Robert Beaubien

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

您不能使用 for..each 循环来删除项目。删除第一项后,for...each 已损坏。

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

回答by Dennis

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

回答by Mark Hall

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

回答by Simon Canning

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

回答by Ameer Chand

Dim x As Integer = 0

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