How to Add and Remove Items in ListBox control at runtime in vb.net

Following example show how to add and remove items in ListBox control at runtime.

This example requires two ListBox controls Named ListBox1 and ListBox2, and two button control with name ButtonSelect and ButtonDeselect, arrange these control on the windows form such as:

When we click on Button Select then Selected item of ListBox1 will be add to ListBox2 after removing from ListBox1 control.And we clcik on ButtonDeselect then selected item of Listbox2 will go to ListBox1 again.

Code:

    Private Sub BindListBox1()
        ListBox1.Items.Add("New Delhi")
        ListBox1.Items.Add("Tokyo")
        ListBox1.Items.Add("Newyork")
        ListBox1.Items.Add("Sydney")
        ListBox1.Items.Add("Paris")
        ListBox1.Items.Add("Londan")
    End Sub
    Private Sub ButtonSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSelect.Click
        If ListBox1.SelectedItem IsNot Nothing Then
            ListBox2.Items.Add(ListBox1.SelectedItem)
            ListBox1.Items.Remove(ListBox1.SelectedItem)
        End If
       End Sub
 
    Private Sub ButtonDeselect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDeselect.Click
        If ListBox2.SelectedItem IsNot Nothing Then
            ListBox1.Items.Add(ListBox2.SelectedItem)
            ListBox2.Items.Remove(ListBox2.SelectedItem)
        End If
    End Sub