Reputation: 9
I'm trying to move multi Items from ListBox1 to ListBox2, but I receive an error message that the below underlined is not a collection type.
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.ListItemCollection
For Each selectedItem In ListBox1.SelectedItem
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox1.Items.Remove(ListBox1.SelectedItem)
Next
Upvotes: 0
Views: 201
Reputation: 634
To check selected items in a listbox, you need to iterate each item and write a condition that asks if the current item is selected or not.
For Each item In ListBox1.Items
If item.selected Then
ListBox2.Items.Add(item)
ListBox1.Items.Remove(item)
End If
Next
System.Web.UI.WebControls.ListControl.Items
returns a collection of items in the control, each Item
has Selected
property which returns a boolean value.
For Windows Form Applications: //Edit [OP is using System.Web.UI.WebControls
SelectedItem
will return only single item.
SelectedItems
will return a collection type based on selected items
Make sure that the SelectionMode
is not One
or None
so instead of using SelectedItem
try to use SelectedItems
Upvotes: 1
Reputation: 841
Change your code like this:
For Each selectedItem In ListBox1.SelectedItems
ListBox2.Items.Add(selectedItem)
ListBox1.Items.Remove(selectedItem)
Next
The ForEach statement needs to apply to a list, so you can retrieve individual items in 'selectedItem' variable. Then you can add/remove that individual item as you like
Upvotes: 1