articles

Home / DeveloperSection / Articles / Move Items from one ListBox to Another in CSharp .NET

Move Items from one ListBox to Another in CSharp .NET

Anonymous User43798 16-Jul-2010
Move Items from one ListBox to Another in CSharp .NET 
To move items from ListBox1 to ListBox2 and vice-versa, we would have to write code for four buttons.

·         Forward: to move selected item in listbox1 to listbox2.

·         Forward All: to move all items from listbox1 to listbox2

·         Remove: to remove item from listbox2 and add in listbox1

·         Remove All: to remove all items in listbox2 and add in listbox1

Forward

 Move Items from one ListBox to Another in CSharp .NET

private void btnforward_Click(object sender, EventArgs e)
        {
            try
            {
                if (listBox1.Items.Count > 0)
                {
// checking whether listbix1 has items or not if yes then moving items
                    listBox2.Items.Add(listBox1.SelectedItem.ToString());
                    listBox1.Items.Remove(listBox1.SelectedItem);
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
//selecting first item of listbox.
            if(listBox1.Items.Count>0)
                listBox1.SelectedIndex = 0;
            listBox2.SelectedIndex = listBox2.Items.Count - 1;
        }

 

Forward All

 Move Items from one ListBox to Another in CSharp .NET

 

        private void btnForwardAll_Click(object sender, EventArgs e)
        {
            try
            {
   //moving all items from listbox1 to listbox2
                listBox2.Items.AddRange(listBox1.Items);
                listBox1.Items.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            listBox2.SelectedIndex = 0;
        }
Remove

 Move Items from one ListBox to Another in CSharp .NET

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                if (listBox2.Items.Count > 0)
{
//checking whether listbox2 has items or not. If yes then moving
//selected item from listbox2 to listbox1
                    listBox1.Items.Add(listBox2.SelectedItem.ToString());
                    listBox2.Items.Remove(listBox2.SelectedItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (listBox2.Items.Count > 0)
                listBox2.SelectedIndex = 0;
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
 
        }

 

Remove All

 Move Items from one ListBox to Another in CSharp .NET

        private void btnRemoveAll_Click(object sender, EventArgs e)
        {
            try
            {
//moving all items from listbox2 to listbox1
                listBox1.Items.AddRange(listBox2.Items);
                listBox2.Items.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
            listBox1.SelectedIndex = 0;
 
        }

 

       

 

 


Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By