Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:36:35+00:00 2026-05-14T02:36:35+00:00

Hi all I have this code to check if an item from a textbox

  • 0

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.

        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 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& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.TextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& 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:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:36:36+00:00Added an answer on May 14, 2026 at 2:36 am

    ClientListBox.Items.Count is evaluated when the loop starts. So, you might have started out with 3 or more items, but as you remove them, there will eventually be less. My guess is that by the 3rd time through the loop (you’re on i = 2) that there is no longer 3 or more items in ClientListBox. So, the index is out of bounds. This is a common gotcha when you’re removing items from a collection or array.

    The classic way to avoid this problem is to iterate backwards. Something like:

    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
    

    This has happened to everyone!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 339k
  • Answers 339k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Generics and reflection don't mix well, especially for lists. I… May 14, 2026 at 4:26 am
  • Editorial Team
    Editorial Team added an answer Seems like there's no way to do it in Capybara,… May 14, 2026 at 4:26 am
  • Editorial Team
    Editorial Team added an answer docs say: itertools.groupby: It generates a break or new group… May 14, 2026 at 4:26 am

Related Questions

UPDATED: Added one more question (Question #4). Hi all, I'm building myself a custom
Update: See the bottom of this question for a C# workaround. Hi there, Consider
Hi I have written a code based upon a requirement. (field1_6) (field2_30) (field3_16) (field4_16)
IDE = VS7 or 2002 Hi all, I have a really weird problem here.
Hi All I am currently having an issue calling a WCF service from a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.