2010-03-23 82 views
1

大家好我有這個代碼來檢查一個文本框中的項目是否在列表框中,它給我在底部的錯誤。任何想法我做錯了什麼?我從我的項目的另一部分複製它,它正在爲那部分工作,所以我不能看到什麼是錯的。檢查一組文本框中的列表框項目vb.net

 If LocationsSearchTextBox.Text <> "" And LocationListBox.Items.Count > 0 Then 
      tempInt = 0 

      While (tempInt < ClientListBox.Items.Count) 
       If LocationListBox.Items(tempInt).ToString.Contains(LocationsSearchTextBox.Text) = False Then 
        LocationListBox.Items.RemoveAt(tempInt) 
       End If 
       tempInt += 1 
      End While 
     End If 

System.ArgumentOutOfRangeException了未處理 消息= 「的 '2' InvalidArgument =值是無效的 '索引' 參數名:索引」 PARAMNAME = 「索引」 源=「System.Windows。表單「 Stackrrace: 位於C:\ Users \ admin \ Desktop \ Auctioneers \ AuctioneerProject \中的AuctioneerProject.Viewing.LocationsSextTextBox_KeyPress(System.Windows.Forms.ListBox.ObjectCollection.get_Item(Int32 index) ) AuctioneerProject \ Viewing.vb:line 301 at System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e) 在System.Windows.Forms.Control.ProcessKeyEventArgs(消息&米) 在System.Windows.Forms.Control.ProcessKeyMessage(消息&米) 在System.Windows.Forms.Control.WmKeyChar(消息&米) 在系統.Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.TextBoxBase.WndProc(消息&米) 在System.Windows.Forms.TextBox.WndProc(消息&米) 在System.Windows .Forms.Control.ControlNativeWindow.OnMessage(Message & m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message & m) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr的的HWND,MSG的Int32,IntPtr的WPARAM,IntPtr的LPARAM) 在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG & MSG) 在System.Windows.Forms.Application。 Component.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context) at System.Windows.Forms .Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context) at System.Windows.Forms.Application.Run(ApplicationContext context) at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() at AuctioneerProject.My.MyApplication.Main(String [] Args)在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String [] commandLine) 17d14f5c-a337- 4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args) at System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) at Microsoft.VisualStudio System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回調,對象狀態)下System.Threading.ThreadHelper.ThreadStart_Context(對象狀態) 在系統上.HostingProcess.HostProc.RunUsersAssembly() em.Threading.ThreadHelper.ThreadStart() InnerException:

回答

1

ClientListBox.Items.Count在循環開始時進行評估。所以,你可能已經開始了3個或更多的項目,但是當你移除它們時,最終會少一些。我的猜測是,在第三次通過循環(你在i = 2)時,ClientListBox中不再有3個或更多項目。所以,這個指數是超越界限的。當您從集合或數組中刪除項目時,這是一個常見的問題。

避免此問題的經典方法是向後迭代。例如:

Dim tempInt = ClientListBox.Items.Count - 1 
While (tempInt > -1) 
    If LocationListBox.Items(tempInt).ToString.Contains(LocationsSearchTextBox.Text) = False Then 
     LocationListBox.Items.RemoveAt(tempInt) 
    End If 
    tempInt -= 1 
End While 

這發生在每個人身上!

+0

這就是非常正確的感謝 – 2010-03-23 15:09:09

+0

雖然我ju做了一點點不同ST 如果LocationListBox.Items(tempInt).ToString.Contains(LocationsSearchTextBox.Text)= false,那麼 LocationListBox.Items.RemoveAt(tempInt) 其他 tempInt + = 1點 結束如果 – 2010-03-23 15:17:45

+0

你絕對應該這樣做的,如果你'在學校,因爲你總是想要正確,但**不是按照他們期望的方式去做。 – 2010-03-23 16:46:15

1

如果你打算遍歷一個列表,有時從列表中刪除項目,那麼最好是「倒退」。

因此,與tempInt集開始的最大行數:

tempInt = LocationListBox.Items.Count-1 

和tempInt減爲您遍歷清單:

tempInt -=1 

當然while循環,必須修改閱讀:

While (tempInt >= 0) 
+0

+1你應該得到一個upvote呢! – 2010-03-23 15:12:37

相關問題