根据文本框vb.net检查一组列表框项

时间:2010-03-23 14:57:33

标签: .net 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未处理   Message =“InvalidArgument ='2'的值对'index'无效。参数名称:index”   PARAMNAME = “索引”   来源= “System.Windows.Forms的”   堆栈跟踪:        在System.Windows.Forms.ListBox.ObjectCollection.get_Item(Int32索引)        at AuctioneerProject.Viewing.LocationsSearchTextBox_KeyPress(Object sender,KeyPressEventArgs e)位于C:\ Users \ admin \ Desktop \ Auctioneers \ AuctioneerProject \ AuctioneerProject \ Viewing.vb:第301行        在System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e)        在System.Windows.Forms.Control.ProcessKeyEventArgs(消息&amp; m)        在System.Windows.Forms.Control.ProcessKeyMessage(消息&amp; m)        在System.Windows.Forms.Control.WmKeyChar(消息&amp; m)        在System.Windows.Forms.Control.WndProc(消息&amp; m)        在System.Windows.Forms.TextBoxBase.WndProc(消息&amp; m)        在System.Windows.Forms.TextBox.WndProc(消息&amp; m)        在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)        在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)        在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)        在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)        在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)        在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)        在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)        在System.Windows.Forms.Application.Run(ApplicationContext context)        在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()        在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()        在Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String [] commandLine)        at AuctioneerProject.My.MyApplication.Main(String [] Args)in 17d14f5c-a337-4978-8281-53493378c1071.vb:第81行        在System.AppDomain._nExecuteAssembly(Assembly assembly,String [] args)        在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)        在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()        在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)        在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)        在System.Threading.ThreadHelper.ThreadStart()   InnerException:

2 个答案:

答案 0 :(得分:1)

循环开始时会评估

ClientListBox.Items.Count。因此,您可能已经开始使用3个或更多项目,但是当您删除它们时,最终会减少。我的猜测是,通过循环第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

这已经发生在每个人身上!

答案 1 :(得分:1)

如果您要循环列表并有时从列表中删除项目,那么最好“向后”计算。

首先将tempInt设置为最大行数:

tempInt = LocationListBox.Items.Count-1

并在循环列表时从tempInt中减去:

tempInt -=1

当然还必须修改while循环,以便阅读:

While (tempInt >= 0)