By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,152 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Moving multiple items between two ListBoxes

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Dany P. Wu" <da**@nospam.quicksilver.net.nz> wrote in message
news:10***************@drone1-svc-skyt.qsi.net.nz...
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.

Nov 21 '05 #2

P: n/a
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

"Dany P. Wu" <da**@nospam.quicksilver.net.nz> wrote in message
news:10***************@drone1-svc-skyt.qsi.net.nz...
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.

Nov 21 '05 #3

P: n/a
"Richard Myers" <ri*********************@basd.co.nz> wrote in message
news:eW**************@TK2MSFTNGP11.phx.gbl...
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.
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.