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"