In today's article I will show you a simple tutorial with example how you can move or transfer or two‑way ListBox Items Between ListBoxes in C# & VB.NET (WinForms). This article you can use in .net core also. So, for this article first we will create a new windows application using c#.net (here we will show code for VB.net). Now add a windows form and add list box control as shown below.
Now let's genera the click event and add the below code.
C#.Net
private void btnTransferRight_Click(object sender, EventArgs e)
{
foreach (var item in new ArrayList(listBox1.SelectedItems))
{
listBox1.Items.Remove(item);
listBox2.Items.Add(item);
}
}
private void btnTransferLeft_Click(object sender, EventArgs e)
{
foreach (var item in new ArrayList(listBox2.SelectedItems))
{
listBox2.Items.Remove(item);
listBox1.Items.Add(item);
}
}
VB.Net
Private Sub btnTransferRight_Click(sender As Object, e As EventArgs)
For Each item In New ArrayList(listBox1.SelectedItems)
listBox1.Items.Remove(item)
listBox2.Items.Add(item)
Next
End Sub
Private Sub btnTransferLeft_Click(sender As Object, e As EventArgs)
For Each item In New ArrayList(listBox2.SelectedItems)
listBox2.Items.Remove(item)
listBox1.Items.Add(item)
Next
End Sub
In above code on button click event first i have read all the selected item from the listbox. After that I have removed item the selected list and added to the next list. Here we are moving only single item from one list to another listbox. Now let's run the code to check the output.