473,737 Members | 8,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Selecte dObjectCollecti on 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 7392
Hi Dany.

Dim shiftObjects() as Object
shiftObjects = Me.lstMyList.Se lectedObjects

Dim i as integer
for i = 0 to shiftObjects.le ngth-1
Me.lstMyList.Re move(shiftObjec ts(i))
Me.lstAddToList .Add(shiftObjec ts(i))
next i

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

hth
Richard

"Dany P. Wu" <da**@nospam.qu icksilver.net.n z> 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.Selecte dObjectCollecti on 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 SelectedObjectC ollection 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(ind ex) 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.qu icksilver.net.n z> 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.Selecte dObjectCollecti on 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******** ******@TK2MSFTN GP11.phx.gbl...
Hi Dany.
Dim shiftObjects() as Object
shiftObjects = Me.lstMyList.Se lectedObjects
Dim i as integer
for i = 0 to shiftObjects.le ngth-1
Me.lstMyList.Re move(shiftObjec ts(i))
Me.lstAddToList .Add(shiftObjec ts(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.Selecte dObjectCollecti on 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.Selecte dObjectColletio n

items = lb1.SelectedIte ms
While items.Count > 0
lb2.Items.Add(i tems(0))
lb1.Items.Remov e(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
2100
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 like in Windows Explorer - CTRL and SHIFT keys for file selection)? Or maybe someone knows the tree component available to download that supports multiple items selections? Then I will be able to take a look at the code and see how the multiple...
1
6328
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 <ASP:DROPDOWNLIST ID="lbCountriesWiz" ENABLEVIEWSTATE="true" FONT-SIZE="8pt" ONSELECTEDINDEXCHANGED="ddlQueryProvinces" AUTOPOSTBACK="True" RUNAT="server" WIDTH="150px"></ASP:DROPDOWNLIST> Private Sub Page_Load(ByVal sender As System.Object, ByVal...
1
1885
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 SelectedValue for listbox, it will only allow to bind to one value? Does anyone know how to bind select multiple items for each listbox? -------------------------------- From: Isaac Wang ----------------------- Posted by a user from .NET 247...
1
2920
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 have written the code using radio button for selecting single item.but we want to replace it with checkbox to select multiple items. the code using radio button is given below .pls correct it with checkbox
2
8152
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 "Shift" or "Ctrl" buttons to select multiple items from LIST 1 and drag them to LIST 2 in one go. I've already have the code to drag and drop between lists but for 1 item at a time. The only method to move multiple items between lists
2
15252
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 using findbytext method.I got the following error ERROR: Cannot have multiple items selected in a DropDownList. Coding is below
4
2221
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 tell that what's the mistake in my code. <td><select name="imagelist" id="imagelist" multiple="multiple"> <? while($row=mysql_fetch_array($result)) {
1
4886
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 in the list box). I need to it also select multiple sets of records (Multi-Select = Extended). I modified my code based on code I found on some Internet site which gave an example using three fields in a three field table. It loops through the...
17
20022
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 btnMoverr_Click(object sender, EventArgs e) { //lstBoxr.Items.Add(lstBoxl.SelectedItems); //lstBoxl.Items.Remove(lstBoxr.SelectedItems);
0
9470
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9258
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9207
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8207
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6749
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6052
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.