Tuesday 29 January 2013

move listbox items to another listbox in c#

// all items moves from left listbox to right


int count = listBox1.Items.Count;//assigning items in listbox it into count variable
            if (count != 0)//checking the conditions
            {
                for (int i = 0; i < count; i++)
                {
                    listBox2.Items.Add(listBox1.Items[i]);
                }
            }

            listBox1.Items.Clear();//clear the listbox after transfering records.


//only selected item move to right listbox


if (listBox1.SelectedIndex > -1)
            {
                listBox2.Items.Add(listBox1.SelectedItem);//transfer records from listbox1 to Listbox2 on select records..
                listBox1.Items.Remove(listBox1.SelectedItem);

            }

//only selected item move to left listbox


if (listBox2.SelectedIndex > -1)
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                listBox2.Items.Remove(listBox2.SelectedItem);
            }

// all items moves from right listbox to left


int count = listBox2.Items.Count;
            if (count != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    listBox1.Items.Add(listBox2.Items[i]);
                }
            }
            listBox2.Items.Clear();


1 comment:

  1. Hello sir,
    I want to know how can I add and remove items from one listbox to another listbox? Plz help me.

    ReplyDelete