This Article is by Rajesh

Description:

In this article we will see a simple example to interchange the items between two list boxes.
The screenshot of the application is shown in the image below.

two listbox csharp

The C# code in the code behind file is like this
using System;
using System.Windows.Forms;
namespace ListBox
{
    public partial class From_listbox : Form
    {
        public From_listbox()
        {
            InitializeComponent();
        }
        private void trsfr_B_Click(object sender, EventArgs e)
        {
            string value1=string.Empty;
            try
            {
                //copying selected value in List Box A
                value1 = listBoxA.SelectedItem.ToString();
                //Adding item to the List Box B
                listBoxB.Items.Add(value1);
                //Deleting the item in List Box A
                listBoxA.Items.Remove(listBoxA.SelectedItem);
            }
            catch
            {
                // showing error message if no item is selected in list Box
                MessageBox.Show(“select Item from list”);
            }
        }
        private void trnsfr_A_Click(object sender, EventArgs e)
        {
            string value2 = string.Empty;
            try
            {
                //copying selected value in List Box B
                value2 = listBoxB.SelectedItem.ToString();
                //Adding item to the List Box A
                listBoxA.Items.Add(value2);
                //Deleting the item in List Box B
                listBoxB.Items.Remove(listBoxB.SelectedItem);
            }
            catch
            {
                //showing error message if no item is selected in list Box
                MessageBox.Show(“select the item from list”);
            }
        }
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *