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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DjPal
    New Member
    • Dec 2009
    • 15

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

    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 "{collectio n}" in the other list box.
    Code:
    private void btnMoverr_Click(object sender, EventArgs e)
            {
                
                //lstBoxr.Items.Add(lstBoxl.SelectedItems);
                //lstBoxl.Items.Remove(lstBoxr.SelectedItems);
    
                while (lstBoxl.SelectedItems.Count > 0)
                {
    
                    lstBoxr.Items.Add(lstBoxl.SelectedItems);
                    lstBoxl.Items.Remove(lstBoxl.SelectedItems);
                }
    
    
                //lstBoxr.SelectedItems.Add(lstBoxl.SelectedItems);
                //lstBoxl.SelectedItems.Remove(lstBoxl.SelectedItems);
            }
    i'm using C# in visual studio 2008, any ideas would be appreciated!

    Thanks,
    Pal
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    You iterate through the collection of selected items and display one by one.... Basically you have the collection not individual items...

    Regards
    Dheeraj Joshi

    Comment

    • sanjib65
      New Member
      • Nov 2009
      • 102

      #3
      Listbox

      Try the followng code, hopefully it will work. Actaually ListBox Control has an event called SelectedIndexCh ange(), 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.

      Code:
      using System;
      using System.Collections;
      using System.Configuration;
      using System.Data;
      using System.Linq;
      using System.Web;
      using System.Web.Security;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      using System.Web.UI.WebControls.WebParts;
      using System.Xml.Linq;
      
      public partial class Default3 : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                  ListBox1.Items.Add("anything");
                  ListBox1.Items.Add("State");
                  ListBox1.Items.Add("anything");
                  ListBox1.Items.Add("anything");
              }
          }
          private void LoadListBox()
          {
              ListBox2.Items.Clear();
      
              ListBox2.Items.Add("West bengal");
              ListBox2.Items.Add("Assam");
              ListBox2.Items.Add("Maharashtra");
              ListBox2.Items.Add("Assam");
      
          }
          protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
          {
              if (ListBox1.SelectedIndex == 1)
              {
                  LoadListBox();
              }
          }
      }

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        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

        Comment

        • DjPal
          New Member
          • Dec 2009
          • 15

          #5
          Thanks guys.. still havn't solved the issue yet but incase you think of anything more let me know! cheers.

          Comment

          • Dheeraj Joshi
            Recognized Expert Top Contributor
            • Jul 2009
            • 1129

            #6
            Basically you want to move items(Selected items of course) from one list box to other....? Am i right..?

            Regards
            Dheeraj Joshi

            Comment

            • DjPal
              New Member
              • Dec 2009
              • 15

              #7
              Yes Dheeraj,
              I have two list boxes, and i want to move multiple items (yes, selected) from one list box to another!

              Comment

              • sanjib65
                New Member
                • Nov 2009
                • 102

                #8
                Moving ListBox

                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.

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  There is a property of "SelectedIndexC ollection". 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.

                  Comment

                  • DjPal
                    New Member
                    • Dec 2009
                    • 15

                    #10
                    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.A dd(lstBoxr.Text );
                    lstBoxr.Items.R emove(lstBoxr.S electedItems);


                    but it just moves one selected item at a time.

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      Originally posted by DJPal
                      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.Selecte dIndexCollectio n

                      Comment

                      • DjPal
                        New Member
                        • Dec 2009
                        • 15

                        #12
                        sorry im new to programming and dont understand it. im not purposely ignoring advice!

                        Comment

                        • sanjib65
                          New Member
                          • Nov 2009
                          • 102

                          #13
                          Moving Items

                          Try this
                          Code:
                          using System;
                          using System.Collections.Generic;
                          using System.ComponentModel;
                          using System.Data;
                          using System.Drawing;
                          using System.Linq;
                          using System.Text;
                          using System.Windows.Forms;
                          
                          namespace WindowsFormsApplication1
                          {
                              public partial class Form1 : Form
                              {
                                  public Form1()
                                  {
                                      InitializeComponent();
                                      listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
                                  }
                                  
                          
                                  private void button1_Click(object sender, EventArgs e)
                                  {
                                      
                                      foreach (int i in listBox1.SelectedIndices)
                                      {
                                          listBox2.Items.Add(listBox1.Items[i].ToString());                
                                      }
                                  }
                              }
                          }
                          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.

                          Comment

                          • DjPal
                            New Member
                            • Dec 2009
                            • 15

                            #14
                            Originally posted by sanjib65
                            Try this
                            Code:
                            using System;
                            using System.Collections.Generic;
                            using System.ComponentModel;
                            using System.Data;
                            using System.Drawing;
                            using System.Linq;
                            using System.Text;
                            using System.Windows.Forms;
                            
                            namespace WindowsFormsApplication1
                            {
                                public partial class Form1 : Form
                                {
                                    public Form1()
                                    {
                                        InitializeComponent();
                                        listBox1.Items.AddRange(new String[] { "Item1", "Item2", "Item3", "Item4" });
                                    }
                                    
                            
                                    private void button1_Click(object sender, EventArgs e)
                                    {
                                        
                                        foreach (int i in listBox1.SelectedIndices)
                                        {
                                            listBox2.Items.Add(listBox1.Items[i].ToString());                
                                        }
                                    }
                                }
                            }
                            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.
                            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.Selecte dIndices)
                            {
                            lstBoxr.Items.A dd(lstBoxl.Item s[i].ToString());
                            lstBoxl.Items.R emove(lstBoxl.I tems[i]);
                            }
                            }

                            Any ideas??

                            thanks so much for your help.

                            Comment

                            • tlhintoq
                              Recognized Expert Specialist
                              • Mar 2008
                              • 3532

                              #15
                              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.

                              Comment

                              Working...