Vb.net 从列表框中删除Visual basic 2010

Vb.net 从列表框中删除Visual basic 2010,vb.net,vb.net-2010,Vb.net,Vb.net 2010,我很难弄清楚我做错了什么。 基本上,我需要删除比avarage少的Listbox1项,但它给了我: System.ArgumentOutOfRangeException未处理 Message=InvalidArgument=值“9”对“index”无效。 参数名称:索引 Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click Dim

我很难弄清楚我做错了什么。 基本上,我需要删除比avarage少的Listbox1项,但它给了我:

System.ArgumentOutOfRangeException未处理 Message=InvalidArgument=值“9”对“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
Private子标签3\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理标签3。单击
Dim Myrand作为新的随机变量
双色调暗
作为整数的Dim i
作为整数的Dim n
双倍
双倍
ListBox1.Items.Clear()
对于i=0至14,步骤1
res=数学圆(Myrand.NextDouble,3)
列表框1.Items.Add(res)
tot=tot+res
下一个
avarage=tot/ListBox1.Items.Count
MsgBox(avarage)
对于i=0的列表框1.Items.Count-1步骤1
如果列表框1.Items(i)

有什么建议吗?

当您删除某个项目时,它不再在列表中,因此列表会变短,并且您的原始计数不再有效。只需减少i

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
        res = Math.Round(Myrand.NextDouble, 3)
        ListBox1.Items.Add(res)
        tot += res
    Next

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

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

    MsgBox("Removed " & n & " items!")
End Sub
Private子标签3\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理标签3。单击
Dim Myrand作为新的随机变量
双色调暗
作为整数的Dim i
作为整数的Dim n
双倍
双倍
ListBox1.Items.Clear()
对于i=0到14
res=数学圆(Myrand.NextDouble,3)
列表框1.Items.Add(res)
tot+=res
下一个
avarage=tot/ListBox1.Items.Count
MsgBox(avarage)
对于ListBox1.Items.Count-1的i=0
如果列表框1.Items(i)
它在For/Next循环开始时获取最大计数,并将其删除。尝试反向迭代,这样你就可以从你曾经去过的地方而不是你要去的地方移除

i、 e

对于i=ListBox1.Items.Count-1到0步骤-1
如果列表框1.Items(i)
从上面的MSDN链接重点:

当For…Next循环开始时,Visual Basic会计算开始、结束和结束 步这是它计算这些值的唯一时间。然后分配 开始反击。在运行语句块之前,它会比较 背道而驰。如果计数器已大于结束值(或 如果步长为负,则越小),For循环结束,控制传递到 下一个语句后面的语句。否则,声明 街区跑步


即使通过减少i,我也有相同的错误:System.ArgumentOutOfRangeException未经处理消息=InvalidArgument=值“7”对“index”无效。参数名称:Index感谢您帮助并简化了我的代码!当我改变下一个循环时,它现在起作用了。是的,这是一个陷阱。。。谢谢
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