473,386 Members | 1,773 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,386 software developers and data experts.

List Box : how to move multiple items in C# ?

DjPal
15
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.
Expand|Select|Wrap|Line Numbers
  1. private void btnMoverr_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             //lstBoxr.Items.Add(lstBoxl.SelectedItems);
  5.             //lstBoxl.Items.Remove(lstBoxr.SelectedItems);
  6.  
  7.             while (lstBoxl.SelectedItems.Count > 0)
  8.             {
  9.  
  10.                 lstBoxr.Items.Add(lstBoxl.SelectedItems);
  11.                 lstBoxl.Items.Remove(lstBoxl.SelectedItems);
  12.             }
  13.  
  14.  
  15.             //lstBoxr.SelectedItems.Add(lstBoxl.SelectedItems);
  16.             //lstBoxl.SelectedItems.Remove(lstBoxl.SelectedItems);
  17.         }
  18.  
i'm using C# in visual studio 2008, any ideas would be appreciated!

Thanks,
Pal
Dec 4 '09 #1

✓ answered by sanjib65

What tlhintoq had said earlier is the proper way. You have to go for a second loop in reverse order. For fun I coded this way outside the first loop:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
  18.         }
  19.  
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.  
  24.             foreach (int i in listBox1.SelectedIndices)
  25.             {
  26.                 listBox2.Items.Add(listBox1.Items[i].ToString());               
  27.             }
  28.             listBox1.Items.Remove(listBox1.Items[3].ToString());
  29.             listBox1.Items.Remove(listBox1.Items[2].ToString());
  30.             listBox1.Items.Remove(listBox1.Items[1].ToString());
  31.             listBox1.Items.Remove(listBox1.Items[0].ToString());
  32.  
  33.  
  34.  
  35.  
  36.  
  37.         }
  38.     }
  39. }
  40.  
  41.  

17 19980
Dheeraj Joshi
1,123 Expert 1GB
You iterate through the collection of selected items and display one by one.... Basically you have the collection not individual items...

Regards
Dheeraj Joshi
Dec 4 '09 #2
sanjib65
102 100+
Try the followng code, hopefully it will work. Actaually ListBox Control has an event called SelectedIndexChange(), to fire it I have used another Button but wrote nothing in its eventhandler because the Page_Load() method is !IsPostback. So when you select State in the first ListBox1, and click the Button, the second ListBox2 will take the value from the first one.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13.  
  14. public partial class Default3 : System.Web.UI.Page
  15. {
  16.     protected void Page_Load(object sender, EventArgs e)
  17.     {
  18.         if (!IsPostBack)
  19.         {
  20.             ListBox1.Items.Add("anything");
  21.             ListBox1.Items.Add("State");
  22.             ListBox1.Items.Add("anything");
  23.             ListBox1.Items.Add("anything");
  24.         }
  25.     }
  26.     private void LoadListBox()
  27.     {
  28.         ListBox2.Items.Clear();
  29.  
  30.         ListBox2.Items.Add("West bengal");
  31.         ListBox2.Items.Add("Assam");
  32.         ListBox2.Items.Add("Maharashtra");
  33.         ListBox2.Items.Add("Assam");
  34.  
  35.     }
  36.     protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
  37.     {
  38.         if (ListBox1.SelectedIndex == 1)
  39.         {
  40.             LoadListBox();
  41.         }
  42.     }
  43. }
  44.  
Dec 4 '09 #3
Dheeraj Joshi
1,123 Expert 1GB
Hey sanjib65,

You are not moving the items from one list box to other....

Selected index changed occurs when property of selected item changes. How you are relating this to the problem?

Regards
Dheeraj Joshi
Dec 4 '09 #4
DjPal
15
Thanks guys.. still havn't solved the issue yet but incase you think of anything more let me know! cheers.
Dec 4 '09 #5
Dheeraj Joshi
1,123 Expert 1GB
Basically you want to move items(Selected items of course) from one list box to other....? Am i right..?

Regards
Dheeraj Joshi
Dec 4 '09 #6
DjPal
15
Yes Dheeraj,
I have two list boxes, and i want to move multiple items (yes, selected) from one list box to another!
Dec 4 '09 #7
sanjib65
102 100+
Sorry friends, probably I was in hurry and did not follow the problem. Actually what he wanted to do is clearing one ListBox and moving the Items from first to second. Have I understood it now properly? Please let me know so that I can have a try.
Dec 4 '09 #8
tlhintoq
3,525 Expert 2GB
There is a property of "SelectedIndexCollection". This will provide a list of numbers that are the indexes of the selected times of the ListBox. For example: if the first, third and tenth items were selected you would get indexes of 0,2,9

I suggest, copy the selected items to the second ListBox in one loop then remove the items *in reverse order* during a second loop.

The reason you remove in reverse order is because as soon you remove item 2, item 3 becomes 2, 4 becomes 3 and so on, invalidating the list. But if you take away 9, then 3, then 0 you don't face that problem.
Dec 4 '09 #9
DjPal
15
Hi
sanjib65

the aim is to move multiple selected items from one listbox to another listbox. i have tried the following code;

private void btnMovell_Click(object sender, EventArgs e)
{
lstBoxl.Items.Add(lstBoxr.Text);
lstBoxr.Items.Remove(lstBoxr.SelectedItems);


but it just moves one selected item at a time.
Dec 4 '09 #10
tlhintoq
3,525 Expert 2GB
the aim is to move multiple selected items from one listbox to another listbox. i have tried the following code;
You are not listening to the advice being given you.

You have to go through the ListBox.Items collection in a loop and move the specific items one at a time.

MSDN On the ListBox.SelectedIndexCollection
Dec 4 '09 #11
DjPal
15
sorry im new to programming and dont understand it. im not purposely ignoring advice!
Dec 4 '09 #12
sanjib65
102 100+
Try this
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
  18.         }
  19.  
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.  
  24.             foreach (int i in listBox1.SelectedIndices)
  25.             {
  26.                 listBox2.Items.Add(listBox1.Items[i].ToString());                
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32.  
What I've done is very simple. Adding 4 Items in listBox1 first, so that when the form opens up listBox1 is full. Now selecting all Items just clicking the button I move all items at once to listBox2.
Dec 5 '09 #13
DjPal
15
@sanjib65
hi sanjib65,

brilliant! now everything i select is copied to the other listbox, but its not a 'move' since the items in the original listbox are still there after the move.

so i modified the code as below but, all the items don't get removed! strange.

private void btnMoverr_Click(object sender, EventArgs e)
{
foreach (int i in lstBoxl.SelectedIndices)
{
lstBoxr.Items.Add(lstBoxl.Items[i].ToString());
lstBoxl.Items.Remove(lstBoxl.Items[i]);
}
}

Any ideas??

thanks so much for your help.
Dec 5 '09 #14
tlhintoq
3,525 Expert 2GB
Any ideas??
Yeah, go back and read the earlier advice telling you to NOT do it that way. First copy everything (in one loop) THEN remove everything selected in REVERSE order (in a second loop) so you don't screw up the indexes of the selected items.
Dec 6 '09 #15
sanjib65
102 100+
What tlhintoq had said earlier is the proper way. You have to go for a second loop in reverse order. For fun I coded this way outside the first loop:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
  18.         }
  19.  
  20.  
  21.         private void button1_Click(object sender, EventArgs e)
  22.         {
  23.  
  24.             foreach (int i in listBox1.SelectedIndices)
  25.             {
  26.                 listBox2.Items.Add(listBox1.Items[i].ToString());               
  27.             }
  28.             listBox1.Items.Remove(listBox1.Items[3].ToString());
  29.             listBox1.Items.Remove(listBox1.Items[2].ToString());
  30.             listBox1.Items.Remove(listBox1.Items[1].ToString());
  31.             listBox1.Items.Remove(listBox1.Items[0].ToString());
  32.  
  33.  
  34.  
  35.  
  36.  
  37.         }
  38.     }
  39. }
  40.  
  41.  
Dec 6 '09 #16
DjPal
15
Thanks a lot everyone! Managed in the end!
Dec 7 '09 #17
vry nice
vry neatly explained
u may also read this:
Move Items from one ListBox to another in CSharp .NET
Dec 26 '14 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: gabe_pu | last post by:
List Box Multiple Selections I have a list box on my page and IŽm trying to add new items on it. The insert operation must be under the selected item. When I make the insertion it is done ...
2
by: Peter | last post by:
Hello! Please, could anyone tell, is it possible to set multiple items to be selected in list control in the code? For example when the web form is loaded three items of 5 are selected in list...
0
by: Robin Tucker | last post by:
Apologies for the report, but this problem is doing my head in! : I have a list box (just happens to be owner draw). When I select multiple items using the CTRL key, the items I have selected...
3
by: Dany P. Wu | last post by:
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...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.