列表框VB.NET中的索引超出范围

列表框VB.NET中的索引超出范围,vb.net,Vb.net,考虑以下代码: Private Sub DelButton_Click(sender As Object, e As EventArgs) Handles DelButton.Click If OrderListBox.Text = "" = False Then For i As Integer = OrderListBox.SelectedIndex To arr.Count - 1 arr.RemoveAt(i) Ne

考虑以下代码:

Private Sub DelButton_Click(sender As Object, e As EventArgs) Handles DelButton.Click

    If OrderListBox.Text = "" = False Then

        For i As Integer = OrderListBox.SelectedIndex To arr.Count - 1

            arr.RemoveAt(i)

        Next


        OrderListBox.Items.Remove(OrderListBox.SelectedItem)

    End If

    calculate()

End Sub
程序在arr.RemoveAt(i)时崩溃,并显示以下错误:

索引超出范围。必须为非负数且小于尺寸 收藏的一部分


当您尝试删除索引大于集合大小的项时,将引发此错误。i、 e当
i
大于
arr.Count-1

您应该确保
OrderListBox.SelectedIndex
不大于
arr.Count-1
。因为如果它真的存在,你就会删除一个不存在的项目

此代码实际上显示在中。如上所述,您应该这样做:

   Private Sub RemoveTopItems()
   ' Determine if the currently selected item in the ListBox  
   ' is the item displayed at the top in the ListBox. 
   If listBox1.TopIndex <> listBox1.SelectedIndex Then 
   ' Make the currently selected item the top item in the ListBox.
   listBox1.TopIndex = listBox1.SelectedIndex
   End If 
   ' Remove all items before the top item in the ListBox. 
   Dim x As Integer 
   For x = listBox1.SelectedIndex - 1 To 0 Step -1
      listBox1.Items.RemoveAt(x)
   Next x

   ' Clear all selections in the ListBox.
   listBox1.ClearSelected()
End Sub 'RemoveTopItems
Private子RemoveTopItems()
'确定列表框中当前选定的项目
'是显示在列表框顶部的项目。
如果listBox1.TopIndex listBox1.SelectedIndex,则
'使当前选定的项目成为列表框中的顶部项目。
listBox1.TopIndex=listBox1.SelectedIndex
如果结束
'删除列表框中顶部项目之前的所有项目。
作为整数的Dim x
对于x=listBox1.SelectedIndex-1至0步骤-1
列表框1.Items.RemoveAt(x)
下一个x
'清除列表框中的所有选择。
listBox1.ClearSelected()
“拆除”终端组件
将返回一个负值,如果未选择任何项目,则返回(-1)

您没有在代码中签入它

将代码更改为:

If OrderListBox.SelectedIndex >= 0 And OrderListBox.Text = "" = False Then
编辑

您的代码如下:

 For i As Integer = OrderListBox.SelectedIndex To arr.Count - 1

        arr.RemoveAt(i)

 Next
假设您的
OrderListBox
包含3项:[A、B、C],并且
SelectedIndex
为0

然后,您的代码将:

  • 移除(0)==>[B,C]
  • 移除(1)==>[B]
  • 删除(2)==>异常
你需要逆转这个循环

 For i As Integer = arr.Count - 1 To OrderListBox.SelectedIndex Step -1

        arr.RemoveAt(i)

 Next

首先,请注意,在VB.NET和C#中,FOR循环的实现方式不同

在VB.NET中,其工作原理如下:

   Private Sub RemoveTopItems()
   ' Determine if the currently selected item in the ListBox  
   ' is the item displayed at the top in the ListBox. 
   If listBox1.TopIndex <> listBox1.SelectedIndex Then 
   ' Make the currently selected item the top item in the ListBox.
   listBox1.TopIndex = listBox1.SelectedIndex
   End If 
   ' Remove all items before the top item in the ListBox. 
   Dim x As Integer 
   For x = listBox1.SelectedIndex - 1 To 0 Step -1
      listBox1.Items.RemoveAt(x)
   Next x

   ' Clear all selections in the ListBox.
   listBox1.ClearSelected()
End Sub 'RemoveTopItems
在循环开始之前,您要确定循环的开始和结束:

  • Start=OrderListBox.SelectedIndex
  • 结束=arr.Count-1
然后,循环开始

重要的是要知道,在VB.NET中,循环结束不再计算。这是C#的一个重要区别。在C#中,循环的结束在每个循环之前计算

现在,在循环中,您正在从数组中删除记录。
因此,数组中的记录计数正在减少。 但是,循环仍在继续,因为在循环开始之前已经计算了数组中的记录数

因此,您超出了数组的范围

您可以按如下方式重写代码:

   Dim i as Integer
   i = OrderListBox.SelectedIndex
   while i < arr.Count
       arr.RemoveAt(i)
   Next
Dim i作为整数
i=OrderListBox.SelectedIndex
当我数着
arr.RemoveAt(i)
下一个

本文详细介绍了VB.NET中的for循环,特别是“技术实现”一节:

你的意思是什么?你能解释得更多吗@HenkHoltermani我已经尝试过了,但仍然给出了相同的错误arr大小与列表框大小相同吗?是的,曾经添加到数组列表中的内容被添加到arr,然后尝试将i As Integer=OrderListBox的
更改为i As Integer=OrderListBox.SelectedIndex to arr.Count-1
更改为
For i As Integer=OrderListBox.SelectedIndex To OrderListBox.Items.Count-1查看结果这不会修复它。错误的原因是他正在从数组中删除记录,从而减小了数组的大小,但他正在循环开始之前确定数组的大小。请注意,FOR循环在VB.NET和C#中的实现方式不同@HenkHolterman不,您不需要,因为数组在每次循环迭代后都在缩小!