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 7863149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:23:19+00:00 2026-06-02T23:23:19+00:00

Well i got some hard time figuring out what am I doing wrong. Basically

  • 0

Well i got some hard time figuring out what am I doing wrong.
Basically I need to remove Listbox1 items that are less then avarage, but its giving me:

System.ArgumentOutOfRangeException was unhandled
Message=InvalidArgument=Value of ‘9’ is not valid for ‘index’.
Parameter name: index

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
    Dim Myrand As New Random
    Dim res As Double
    Dim i As Integer
    Dim n As Integer
    Dim tot As Double
    Dim avarage As Double

    ListBox1.Items.Clear()

    For i = 0 To 14 Step 1
        res = Math.Round(Myrand.NextDouble, 3)
        ListBox1.Items.Add(res)
        tot = tot + res
    Next

    avarage = tot / ListBox1.Items.Count
    MsgBox(avarage)

    For i = 0 To ListBox1.Items.Count - 1 Step 1
        If ListBox1.Items(i) < avarage Then
            ListBox1.Items.RemoveAt(i)
            n = n + 1
        End If
    Next

    MsgBox("Removed " & n & " items!")
End Sub

Any suggestions?

  • 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-06-02T23:23:21+00:00Added an answer on June 2, 2026 at 11:23 pm

    It is grabbing the max Count at the start of the For/Next Loop and doesn’t reevaluate it. Try iterating backwards that way you are removing from where you have been not from where you are going.

    i.e.

    For i = ListBox1.Items.Count - 1 To 0 Step -1
        If ListBox1.Items(i) < avarage Then
            ListBox1.Items.RemoveAt(i)
            n = n + 1
        End If
    Next
    

    From above MSDN Link emphasis mine:

    When a For…Next loop starts, Visual Basic evaluates start, end, and
    step. This is the only time it evaluates these values. It then assigns
    start to counter. Before it runs the statement block, it compares
    counter to end. If counter is already larger than the end value (or
    smaller if step is negative), the For loop ends and control passes to
    the statement following the Next statement. Otherwise the statement
    block runs.

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

Sidebar

Related Questions

I've got some 3rd party beans that have method signatures that fit quite well
The StateManager in Ember.js isn't that well documented yet, so I've got some questions
Well ive got this code that creates a table from JSON data. If a
I've got an after insert trigger that works well. However, I want it to
Well, now that I've finally got my stupid ODBC stuff configured, I took a
I've got some code that puts a line-through on a TR for deleted rows,
I've got some things I do in the OnActionExecuting method in a BaseController, that
So, I've got some history tables that have a begin date and an end
Well I found this code , after running this I got some activity (as
I have got some XML files that contain comments above the nodes. When I

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.