473,544 Members | 1,792 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 7368
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
2083
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...
1
6300
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"...
1
1883
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? ...
1
2903
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...
2
8126
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...
2
15244
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
2212
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"> <? ...
1
4871
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...
17
19996
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); ...
0
7416
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7761
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7363
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...
0
7701
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...
1
5289
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...
0
4906
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...
0
3403
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...
1
1833
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.