我想通過在VB.net 2008列表視圖搜索它正常工作與小單子,但如果該列表是大(約25000行),如果我搜索多個項目,它沒有說刪除項目索引無效。很明顯,我明白的是,它試圖刪除索引不存在。但我無法弄清楚它到底發生了什麼問題。任何人都可以幫忙嗎?從一個ListView
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
感謝。
Err爲什麼vb.net如此羅嗦,討厭那個!無論如何,你可以一步一步使用調試器? – JonH 2010-02-01 18:07:06
錯誤消息框是:28097的= InvalidArgument值不適用於索引和汽車窗口有這些值:I = 26897,indexToRemove = 28097,indicesToRemove =計數= 26898,indicesToRemove.Count = 26898 – JPro 2010-02-01 18:11:24
那不幫助我。你將不得不給我們它正在炸燬的線路。列表框索引是否有有效的值?你有沒有檢查它是否拋出某種空的異常。這究竟在哪裏爆炸呢? – JonH 2010-02-01 18:15:09