Wednesday, 26 September 2012

moving items from one listbox to another c# windows application


        List<string> list = new List<string>();
        List<string> l1 = new List<string>();
        private void Form1_Load(object sender, EventArgs e)
        {
            list.Add("hey");
            list.Add("you");
            list.Add("over");
            list.Add("there");

            listBox1.DataSource = list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RemoveElements();
        }

        private void RemoveElements()
        {
        
            foreach (var item in listBox1.SelectedItems)
            {
                l1.Add(item.ToString());
                list.Remove(item.ToString());
            }

            listBox1.DataSource = null;
            listBox1.DataSource = list;
            listBox2.DataSource = null;
            listBox2.DataSource = l1;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            RemoveElements();
        }