articles

Home / DeveloperSection / Articles / LISTBOX APPLICATION IN C#

LISTBOX APPLICATION IN C#

Amit Singh14743 12-Sep-2010

In this Application we have to take two listbox1 and listbox2 and four buttons in the form as shown below in the application, our motive is to transfer all the records from ListBox1 to ListBox2 by pressing (>>) and vice versa (<<), and we can transfer one by one record from Listbox1 to Listbox 2 by pressing (>) and vice versa (<) as shown below.

LISTBOX APPLICATION IN C#

First step is to take a two ListBox from the control as Listbox1 and Listbox2, next take Four Buttons as shown above. After taking Listbox1 add items (data) from the properties windows and click on the ellipse (…) button besides the items options. This will open a string Collection Editor Window as shown below.

LISTBOX APPLICATION IN C#

Now place a code of their respected Click event of Button as shown in above figure.

 //This code will transfer all the records inside the Listbox
privatevoid btnLeftTransfer_Click(object sender, EventArgs e)
        {
            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.
        }
 
 
        privatevoid Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        privatevoid btnRightTransferAll_Click(object sender, EventArgs e)
        {
            int count = listBox2.Items.Count;
            if (count != 0)
            {
                for (int i = 0; i < count; i++)
                {
                    listBox1.Items.Add(listBox2.Items[i]);
                }
            }
           listBox2.Items.Clear();
        }
 
     privatevoid btnSingleSelectTransfer_Click(object sender, EventArgs e)
        {
Checking Conditions of selection of listbox.
            if (listBox1.SelectedIndex > -1)
            {
                listBox2.Items.Add(listBox1.SelectedItem);//transfer records from listbox1 to Listbox2 on select records..
                listBox1.Items.Remove(listBox1.SelectedItem);
             
            }
        }
//code to transfer records from listbox2 to listbox1 on slect records.
        privatevoid btnSingleRightTransfer_Click(object sender, EventArgs e)
        {
            if (listBox2.SelectedIndex > -1)
            {
                listBox1.Items.Add(listBox2.SelectedItem);
                listBox2.Items.Remove(listBox2.SelectedItem);
            }
        }
 
  Output of the Following application as shown below.


LISTBOX APPLICATION IN C#

As soon as we pressed(>>) button it will transfer all records from Listbox1 to Listbox2 as shown above ,If a user select single items and pressed(<) button it will show  in listbox2 or in listbox1 depend upon the choice of selected button.

LISTBOX APPLICATION IN C#



Updated 07-Sep-2019

Leave Comment

Comments

Liked By