Home > DeveloperSection > Articles > LISTBOX APPLICATION IN C#

LISTBOX APPLICATION IN C#


C# C# 
Ratings:
0 Comment(s)
 9921  View(s)
Rate this:

LISTBOX APPLICATION IN C#

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

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

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

private void 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.

        }

 

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        private void 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();

        }

 

     private void 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.

        private void 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

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


Don't want to miss updates? Please click the below button!

Follow MindStick