Move or Add Multiple Selected Items Between ListBox Controls (C# & VB.NET)

How to move or dd multiple selected items between listbox controls using c#.net and VB.net. Move Selected Items from One ListBox to Another ListBox.
In today's article I will show you a simple tutorial with example how you can move or transfer or two‑way ListBox multiselect 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.

Listbox exampe

Now to enable the listbox to allow select multiple items we need to set SelectionMode="MultiSimple". 

ListBox SelectionMode

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.

Multi Select Listbox


Post a Comment