Moving multiple items between two ListBoxes

  • Thread starter Thread starter Dany P. Wu
  • Start date Start date
D

Dany P. Wu

Hi everyone,

One of my Windows forms contain two listbox controls, with Add and Remove
buttons between them.

The idea is to allow users to select multiple items from one ListBox, click
the Add button, and the selected items will move to the second ListBox.

I've been trying to use the ListBox.SelectedObjectCollection with no
success. It sounds like the logical thing to use but I can't seem to find
any methods/properties to use in this case. Is there something else I should
be using?

Any comments/suggestions would be greatly appreciated.

Cheers,
Dany.
 
The SelectedObjectCollection is what is returned by SelectedItems as opposed to
an actual property.

You have a few options here to do this.
Basically you need to make a copy of the selected items and add these to the
destination, then through an iterative process remove the selected items from
the source listbox.
A general flow would be to get the SelectedItems collection into another
collection.
You could then add them individually or use AddRange to add the contents of this
collection to the destination listbox.
Then step through each item in the copy of the collection and use .Items.Remove
to remove each item from the source collection.

However, making a copy of the selected items collection is fairly important.
If you try to use For Each to step through items and remove them at the same
time, this will not work. Because you are removing items from the same list you
are iterating over.
Or you could do a For index on each item in the source listbox and check its
GetSelected(index) property. Then handle them appropriately. If you go backwards
through the list then you could add items to the other and remove items from the
source in one pass.

The actual implementation of the above is up to you as there are a number of
ways to do the same thing.

Gerald
 
Hi Dany.

Dim shiftObjects() as Object
shiftObjects = Me.lstMyList.SelectedObjects

Dim i as integer
for i = 0 to shiftObjects.length-1
Me.lstMyList.Remove(shiftObjects(i))
Me.lstAddToList.Add(shiftObjects(i))
next i

Note this just shifts items across but does nothing to persist these
changes.

hth
Richard
 
Richard Myers said:
Hi Dany.
Dim shiftObjects() as Object
shiftObjects = Me.lstMyList.SelectedObjects
Dim i as integer
for i = 0 to shiftObjects.length-1
Me.lstMyList.Remove(shiftObjects(i))
Me.lstAddToList.Add(shiftObjects(i))
next i
Note this just shifts items across but does nothing to persist these
changes.
hth
Richard

Thanks for the suggestions guys. I used ListBox.SelectedObjectCollection to
hold the SelectedItems, and just iterate to move it. Just realised that a
Collection is automatically re-indexed after any addition/deletion - I learn
something new every other day :o)

Dim items as ListBox.SelectedObjectColletion

items = lb1.SelectedItems
While items.Count > 0
lb2.Items.Add(items(0))
lb1.Items.Remove(items(0))
End While

The code above seems to do the trick for me.

Cheers,
Dany.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top