473,287 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 7342
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ringwraith | last post by:
Hi! Have anyone ever tried to modify the Gene Cash's Tkinter tree widget (available from http://home.cfl.rr.com/genecash/) so it supports multiple items selection ( selection of multiple files...
1
by: DC Gringo | last post by:
I'm having such a problem with this DropDownList in a user control that is posting back and throwing an error: System.Web.HttpException: A DropDownList cannot have multiple items selected ...
1
by: Isaac Wang via .NET 247 | last post by:
I am having trouble making preselections for the listboxes in a datagrid. For each row, I have a listbox with selectionmode=multiple, which I bind to a datatable. However, when I used the...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
2
by: anchi.chen | last post by:
Hi People, Just wondering if any of you have ever come across any javascript examples that will allow one to drag and drop multiple items between lists? That is, users would be able to use the...
2
by: =?Utf-8?B?S3Jpc2huYQ==?= | last post by:
Hi, I am devloping one web application using .net framework 2.0.One page has 7 dropdown list control.When i update the values first bind the values to the drop down llist then selected text...
4
by: swethak | last post by:
hi i wrote a code to select multiple items in a drop down list.And i store all the items in my database.But in that i select multiple items and submit that items last item only stored.Please...
1
by: KrazyKasper | last post by:
Access 2003 – Multi-Column List Box – Select Multiple Items I have a multi-column (3 columns) list box that works well to select one set of records or all sets of records (based on the first field...
17
DjPal
by: DjPal | last post by:
Hello, I am trying to move multiple items from one list box to another, i have tried the following code, but the output so far just gives me "{collection}" in the other list box. private void...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.