从ListView中删除项目

时间:2010-02-01 18:04:07

标签: .net vb.net

我正在尝试在VB.net 2008中搜索listview。它适用于小列表,但是当列表很大(大约25000行)时,如果我搜索多个项目,则表示索引无效。显然我理解的是,它试图删除索引不存在。但我无法弄清楚到底出了什么问题。有人可以帮忙吗?

PS:当它正在搜索整个列表视图时,我正在递增index = index+5因为我希望接下来的5行也处于选择状态。

这是代码:

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
    If (e.KeyCode = Keys.PageDown) Then
            'ListView1.Items.Clear()
            Dim s As String
            Dim index As Integer
            Dim item As String


            ListView1.BeginUpdate()

            Try
                ' keep track of the "non-searched items" '
                Dim indicesToRemove As New List(Of Integer)

                ListView1.SelectedIndices.Clear()
                If TextBox1.Text.Length > 0 Then
                    Dim lstOfStrings() As String = TextBox1.Text.Split(","c)

                    For Each s In lstOfStrings

                        For index = 0 To ListView1.Items.Count - 1

                            If s.Trim() <> "" Then
                                item = ListView1.Items(index).ToString()

If item.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) >= 0 Then

                                    ListView1.SelectedIndices.Add(index)
                                    index = index + 5
                                    'ListView1.SelectedIndices.Add(index)


                                Else
                                    ' this item was not searched for; we will remove it '
                                    indicesToRemove.Add(index)
                                End If
                            End If

                        Next

                        ' go backwards to avoid problems with indices being shifted '
                        For i As Integer = indicesToRemove.Count - 1 To 0 Step -1
                            Dim indexToRemove As Integer = indicesToRemove(i)
                    ListView1.Items.RemoveAt(indexToRemove) ' blowing on this line
                        Next

                    Next s

                End If
            Finally
                ListView1.EndUpdate()
            End Try
End Sub

感谢。

2 个答案:

答案 0 :(得分:2)

IMO,在这段代码中有很多东西需要修复......但是一个可以解决你的问题的简单修复是这样的:不是抓住要删除的列表项索引,而是保留一个列表项的数组他们自己,然后只需在每个方法上调用Remove方法。这样你甚至不需要处理索引和订购。

编辑:我认为For Each s In lstOfStrings应该在列表项迭代中嵌套。这可能是问题的一个重要部分。

编辑2:您可能希望让我们了解您尝试使用此代码完成的任务,因为其中有很多内容没有多大意义。

编辑3:我使用ListView,TextBox和Button创建了一个测试项目,并在Form_Load中向ListView添加了一些随机项。这个逻辑对我来说仍然没有100%的意义,但我没有遇到任何崩溃。

编辑4:简化代码。删除了index = index + 5内容。

编辑5:返回其他代码。重新实现了奇怪的索引选择。

编辑6:最后?

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox1.Text.Trim().Length = 0 Then Exit Sub

    ' keep track of the "non-searched items" '
    Dim itemsToRemove As New List(Of ListViewItem)

    ListView1.BeginUpdate()
    ListView1.SelectedIndices.Clear()

    If TextBox1.Text.Length > 0 Then
        Dim lstOfStrings() As String = TextBox1.Text.Split(","c)

        For index As Integer = 0 To ListView1.Items.Count - 1
            For Each s As String In lstOfStrings
                Dim realS As String = s.Trim()

                If realS  "" Then
                    Dim item As ListViewItem = ListView1.Items(index)

                    If item.Text.IndexOf(realS, StringComparison.CurrentCultureIgnoreCase) >= 0 Then
                        Dim i As Integer = 1

                        While (i + index < ListView1.Items.Count) And (i <= 5)
                            ListView1.SelectedIndices.Add(i + index)
                            i = i + 1
                        End While

                        index = index + 5

                        Exit For
                    Else
                        ' this item was not searched for; we will remove it '
                        itemsToRemove.Add(item)
                    End If
                End If
            Next s
        Next index

        For Each i As ListViewItem In itemsToRemove
            ListView1.Items.Remove(i)
        Next i
    End If

    ListView1.EndUpdate()
End Sub

答案 1 :(得分:0)

试试这个。像Jon说的那样,使用listitem本身而不是索引要容易得多。使用for each item循环,您可以随时删除它们。顺便说一下,我推荐某种消息或断点以及try块的结束,特别是那个很大的。

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp

  If (e.KeyCode = Keys.PageDown) Then
    'ListView1.Items.Clear()
    Dim s As String
    Dim item As ListViewItem
    Dim found As Boolean

    ListView1.BeginUpdate()

    Try
        ' keep track of the "non-searched items" '
        Dim indicesToRemove As New List(Of Integer)

        ListView1.SelectedIndices.Clear()
        If TextBox1.Text.Length > 0 Then
          Dim lstOfStrings() As String = TextBox1.Text.Split(","c)
          For Each item In ListView1.Items
            found = False
            For Each s In lstOfStrings
              If String.Compare(s, item.Text, True) = 0 Then
                found = True
                Exit For
              End If
            Next s
          If Not found Then ListView1.Items.Remove(item)
          Next item
        End If

    Catch ex As Exception
      MsgBox(ex.Message)
    Finally
      ListView1.EndUpdate()
    End Try
  End If
End Sub