Hi,
I am trying to search through listview in VB.net 2008. It works fine with small list, but when the list is big ( around 25000 rows), if I search multiple items , it fails saying that index is not valid. Obviously what I understand is , it it tryiong to remove an index does not exist. But I am unable to figure out where exactly it is going wrong. Can anyone please help?
PS : while it is doing search through the entire listview, I am incrementing index = index+5
becasue I want the next 5 rows to be in the selection state as well.
This is the code :
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
Thanks.