1

我有 2 个列表框和一个按钮。每当用户选择 lstAvail 中的项目并按下按钮时,所选项目将被添加到 lstSelected。

如果循环以 1 的步长递减,则以下代码可以正常工作。

Private Sub Form_Load()
Dim intX As Integer

    For intX = 0 To 5
        lstAvail.AddItem "Item #" & Format$(intX, "000")
    Next

End Sub

Private Sub Command1_Click()
Dim intListX As Integer

    For intListX = lstAvail.ListCount - 1 To 0 Step -1
        If (lstAvail.Selected(intListX)) Then
            lstSelected.AddItem lstAvail.List(intListX)
            lstAvail.RemoveItem intListX
        End If
    Next

End Sub

但是为什么我从0递增到(lstAvail.ListCount -1)时它不起作用,如下所示?该代码引发运行时错误“无效的属性数组索引”。

Private Sub Command1_Click()
Dim intListX As Integer
For intListX = 0 To lstAvail.ListCount - 1
        If (lstAvail.Selected(intListX)) Then
            lstSelected.AddItem lstAvail.List(intListX)
            lstAvail.RemoveItem intListX
        End If
Next
End Sub
4

1 回答 1

4

Forwards Loop

The reason why this doesn't work is because you are removing items from <lstAvail>, but you are still incrementing from 0 as if the list still remains as it was.

Items in lstAvail

  • "Item 000"
  • "Item 001"
  • "Item 002"
  • "Item 003"
  • "Item 004"
  • "Item 005"

Let's say you select the third item in the list (ListIndex = 2), and then click the button, triggering off Command1_Click(). You will be incremented intListX until it equals 5. The following is a trace through the For loop:


intListX: 0

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 002"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 1

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 002"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 2

lstAvail.Selected(intListX): True

!! Remove "Item 002" !!

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 3

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 4

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 5

lstAvail.Selected(intListX): <!!Error!!>

There is no item here with index 5.

Reversed Loop

intListX: 5

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 002"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 4

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 002"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 3

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 002"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 2

lstAvail.Selected(intListX): True

!! Remove "Item 002" !!

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 1

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 003"
  • "Item 004"
  • "Item 005"

intListX: 0

lstAvail.Selected(intListX): False

lstAvail:

  • "Item 000"
  • "Item 001"
  • "Item 003"
  • "Item 004"
  • "Item 005"
于 2012-07-24T07:56:55.257 回答