Hi everybody

I have two listboxes.
I have set the selectmode to multiple
but when i select multiple items from listbox1, and then clcik ADD button, only one item is getting added

How can i add all the selected items at a time
I am using

Protected Sub btnAdd_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.ServerClick

Dim licCollection As ListItemCollection

licCollection = New ListItemCollection()
Dim intCount As Integer
For intCount = 0 To lstAllAccounts.Items.Count - 1 Step intCount + 1
If (lstAllAccounts.Items(intCount).Selected = True) Then
licCollection.Add(lstAllAccounts.Items(intCount))
End If
Next
Dim intCount1 As Integer
For intCount1 = 0 To licCollection.Count - 1 Step intCount + 1
lstAllAccounts.Items.Remove(licCollection(intCount1))
lstAccountAccess.Items.Add(licCollection(intCount1))
Next
End Sub

thanks

Recommended Answers

All 11 Replies

But I am using Listbox webcontrol
It dont support Listbox.SelectedItems

Can you please give some other idea?

Thanks

try this:

For Each li As ListItem In lstAllAccounts.Items
If li.Selected Then
lstAllAccounts.Items.Remove(li)
lstAccountAccess.Items.Add(li)
End If
Next

I am sorry to say this that,

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.

is the exception which i encountered on the Next line

When i searched about this exception, it says something about has table.
To be frank i dont understand what is that concept, and looking forward for your great help

thanks

if its a website dont you have to use shift or else it only selects one option?

Yes, with shift only it selects multiple items, othrwise it selects one item

Only IE users need do the shift thing i think

No worry...
Thanks

I got it worked

Sorry I want much help on this one, hope the article helped anyway. Btw it's customary to post the solution that you found, as that will help others who find themselves with the same problem

I am sorry for that
Thanks for making me aware of that
The updated code for adding multiple items to listbox web server control is

Dim licCollection As ListItemCollection
licCollection = New ListItemCollection()
For Each AccountItem As ListItem In listbox1.Items
If AccountItem.Selected Then
licCollection.Add(AccountItem)
End If
Next
Dim intCount1, intcount As Integer
For intCount1 = 0 To licCollection.Count - 1 Step intCount + 1
listbox1.Items.Remove(licCollection(intCount1))
listbox2.Items.Add(licCollection(intCount1))
Next
----

the same code for remove,, just that change the source and target....

Thanks for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.