String.contains不起作用

时间:2014-05-03 21:33:49

标签: vb.net

我尝试根据文本框中的输入过滤列表。如果该项目不包含该字符串,则会从列表中删除该字符串。这是我的子程序:

 Sub filterlists(filter As String)

    Dim removalDifferential As Integer = 0
    For colE As Integer = 0 To RadListView1.Items.Count
        Try
            Dim itemEpp As ListViewDataItem = Me.RadListView1.Items(colE)
            Dim jobname As String = itemEpp(0)

            If Not jobname.Contains(filter) Then

               ' MsgBox(jobname & " Contains " & filter)
                RadListView1.Items.RemoveAt(colE - removalDifferential)
                removalDifferential = removalDifferential + 1
            End If
        Catch
        End Try

    Next
End Sub

目前这不是删除正确的项目。 TRY就在那里,因为当你删除一个项目时,列表索引会发生变化(这意味着for循环长度错误并且会抛出异常错误)。在这里可以使用的任何其他循环选项?

1 个答案:

答案 0 :(得分:2)

假设您确实要删除任何只包含过滤器文本的LVI,您应该通过项目(任何项目,而不仅仅是Listview项目)向后循环,这样索引变量实际上将指向下一个正确的项目。删除:

For n As Integer =  RadListView1.Items.Count-1 to 0 Step -1
    If radListView1.Items(n).Text.Contains(filter) Then
        RadListView1.Items.RemoveAt(n)
    End If
Next