, you can have multiple selections when you set the property to do so. There are different ways to delete all selected items from a
, right or wrong. I have some lessons learned on the topic.
. (It works)
For Each i As ListViewItem In ListView1.SelectedItems
2. Use For Each loop in
. (It doesn’t work)
foreach loop does not work with
ListBox as the way it is does with
ListView. It causes
System.InvalidOperationException: List that this enumerator is bound to has been modified. An enumerator can only be used if the list does not change.
3. A fix to the problem is to copy the selected items to a list.
Dim lst As New List(Of Object)
For Each a As Object In ListBox1.SelectedItems
For Each a As Object In lst
4. Use Index, the right way and the wrong way.
The following snippet works but it must use an inverse loop.
For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
The other way doesn’t work.
But amazingly, this modification works.
For i As Integer = 0 To ListBox1.SelectedIndices.Count - 1
loop. (This would be the best one based on its clear logic)
Do While (ListBox1.SelectedItems.Count > 0)
In fact, the
) is always the first one of the