2010-02-01 24 views
1

我想通過在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 

感謝。

+1

Err爲什麼vb.net如此羅嗦,討厭那個!無論如何,你可以一步一步使用調試器? – JonH 2010-02-01 18:07:06

+0

錯誤消息框是:28097的= InvalidArgument值不適用於索引和汽車窗口有這些值:I = 26897,indexToRemove = 28097,indicesToRemove =計數= 26898,indicesToRemove.Count = 26898 – JPro 2010-02-01 18:11:24

+0

那不幫助我。你將不得不給我們它正在炸燬的線路。列表框索引是否有有效的值?你有沒有檢查它是否拋出某種空的異常。這究竟在哪裏爆炸呢? – JonH 2010-02-01 18:15:09

回答

2

IMO,有很多事情需要修復這個代碼...但一個簡單的解決方案,將解決您的問題是這樣的:而不是抓取列表項索引刪除,保持列表的數組物品本身,然後簡單地調用每個方法的Remove方法。這樣你甚至不需要處理索引和排序。

編輯:我覺得For Each s In lstOfStrings應該嵌套裏面列表項迭代。這可能是問題的重要組成部分。

編輯2:您可能想給我們你想使用此代碼來完成什麼意義,因爲有很多在這回事,它沒有太大的意義。

編輯3:我做了一個試驗項目,一個ListView,一個文本框和按鈕,並增加了一些隨機的項目中的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
+0

陣列意味着列表? – JPro 2010-02-01 18:35:52

+0

是的,類型爲'ListViewItem'。 – 2010-02-01 18:37:55

+0

你能給我示例代碼來存儲列表中的項目從列表視圖? – JPro 2010-02-01 18:48:04

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