467,075 Members | 1,097 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,075 developers. It's quick & easy.

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

DjPal
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.  

  • viewed: 19451
Share:
17 Replies
Dheeraj Joshi
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
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
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
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
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
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
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
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
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
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
sorry im new to programming and dont understand it. im not purposely ignoring advice!
Dec 4 '09 #12
sanjib65
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
@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
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
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
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

Post your reply

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

Similar topics

reply views Thread by gabe_pu | last post: by
2 posts views Thread by Peter | last post: by
reply views Thread by Robin Tucker | last post: by
2 posts views Thread by =?Utf-8?B?S3Jpc2huYQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.