471,089 Members | 1,061 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

Moving multiple items between two ListBoxes

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
3 7168
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
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
"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.

Similar topics

reply views Thread by Ringwraith | last post: by
1 post views Thread by Isaac Wang via .NET 247 | last post: by
2 posts views Thread by =?Utf-8?B?S3Jpc2huYQ==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.