2012-02-04 204 views
1

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

这样做的最好方法是什么?

感谢

回答

4

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

试试这个:

 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 
+0

Normaly你是正确的,它取决于IEnumerable接口是如何实现的。但在这种情况下,它确实工作正常。 – 2012-02-04 08:21:35

2
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 
1

你可以尝试这样的事情。

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

这可能是删除所有列表项的最简单的方法。

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

昏暗的X为整数= 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