Reputation: 115
I want to pass the items from ListBox1 to ListBox2, and delete them from LisBox1. It throws a null exception at "lb2.Items.Add(item)" but can't find out why. It works fine with just one item though
I tried doing a "for each item in lb1.items... lb2.items.add(item) + lb1.items.remove(item)" but it wouldn't work for you can't modify a list while iterating over it or an exception will be thrown. Also tried other different approaches but couldn't make it work
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If lb1.Items.Count > 0 Then
Dim itemsAPasar((lb1.Items.Count - 1)) As Object
For Each item In lb1.Items
itemsAPasar(UBound(itemsAPasar)) = item
Next
For Each item In itemsAPasar
lb2.Items.Add(item)
Next
For Each item In itemsAPasar
lb1.Items.Remove(item)
Next
End If
End Sub
Upvotes: 0
Views: 33
Reputation: 14196
It looks like you could simplify your approach a bit...
Dim itemsToMove = lb1.Items.ToList()
For Each item in itemsToMove
lb1.Items.Remove(item)
lb2.Items.Add(item)
Next
Upvotes: 3