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.
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.
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.
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.
Rahul Roi
19-Oct-2019Very Nice article Sir ji.
Anonymous User
12-Mar-2019Thank You for sharing.
Sushant Mishra
27-Jun-2017I always seek to read your articles.