title: Checking a set of listbox items against a text box vb.net
body: <p>Hi all I have this code to check if an item from a textbox is in a listbox and its giving me the error at the bottom. Any ideas what im doing wrong? I copied it from another part of my project and it was working for that part so I cant see whats wrong.</p>
  
  <pre><code>        If LocationsSearchTextBox.Text &lt;&gt; "" And LocationListBox.Items.Count &gt; 0 Then
              tempInt = 0
  
              While (tempInt &lt; 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
  </code></pre>
  
  <p>System.ArgumentOutOfRangeException was unhandled
    Message="InvalidArgument=Value of '2' is not valid for 'index'. Parameter name: index"
    ParamName="index"
    Source="System.Windows.Forms"
    StackTrace:
         at System.Windows.Forms.ListBox.ObjectCollection.get_Item(Int32 index)
         at AuctioneerProject.Viewing.LocationsSearchTextBox_KeyPress(Object sender, KeyPressEventArgs e) in C:\Users\admin\Desktop\Auctioneers\AuctioneerProject\AuctioneerProject\Viewing.vb:line 301
         at System.Windows.Forms.Control.OnKeyPress(KeyPressEventArgs e)
         at System.Windows.Forms.Control.ProcessKeyEventArgs(Message&amp; m)
         at System.Windows.Forms.Control.ProcessKeyMessage(Message&amp; m)
         at System.Windows.Forms.Control.WmKeyChar(Message&amp; m)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.TextBoxBase.WndProc(Message&amp; m)
         at System.Windows.Forms.TextBox.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
         at System.Windows.Forms.Application.ComponentManager.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()
         at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
         at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
         at AuctioneerProject.My.MyApplication.Main(String[] Args) in 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.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: </p>