This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

23 Jan 2014

Moving Items between Two List Boxes in windows C#

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.

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");

            }        
           
        }
      
    }
}




No comments:

Post a Comment

Thanks for your comments.
-Sameer