删除列表视图中的项目

时间:2013-01-16 19:24:26

标签: .net vb.net listview listviewitem

这似乎很容易,但我现在遇到了这个问题。

我想要做的是在列表视图的第二列中查找特定条目,然后如果找到该特定条目,则删除找到该条目的行。

这是我的代码:

      Dim lvSubItems As ListViewItem.ListViewSubItem

      For Each lvSubItems In lvConnectedClients.Items(lvConnectedClients.Items.Count - 1).SubItems

            If lvSubItems.Text = CType(clientSocket.Client.RemoteEndPoint, IPEndPoint).Address.ToString Then

                  ' This is not the correct way.
                  lvConnectedClients.Items.Remove(lvSubItems)

            End If

      Next

提前致谢!

2 个答案:

答案 0 :(得分:0)

我认为你走在正确的轨道上,下面适用于我(添加“As ListViewItem”)。让我知道。

For Each li As ListViewItem In ListView1.Items

        If li.Text = "test 7" Then

        ListView1.Items.Remove(li)

        End If

Next

我创建项目的代码:

Dim i As Integer = 0

    For i = 0 To 9

        Dim li As New ListViewItem("test " & i)


        ListView1.Items.Add(li)


    Next

答案 1 :(得分:0)

 For Each lvSubItems As Object In ListView1.Items
            If lvSubItems.Text = "test" Then
                ListView1.Items.Remove(lvSubItems)
            End If
 Next

For Each lvSubItems As ListViewItem In ListView1.Items

        If lvSubItems.SubItems(1).Text = "1" Then
            ListView1.Items.Remove(lvSubItems)
        End If
    Next
End Sub