Add, remove And clear items in List Box in vb.net

This article demonstrates how to add, remove and clear items from list box in vb.net. The ListBox control contains ‘Items’ property. You can add and remove items from the list box using this property. Basically, ‘Items’ property is the type of the ‘ListBox.ObjectCollection’ Class which refers the all the items which are held in the Listbox control.

Add items to Listbox

You can add items directly by editing ‘Items’ property from the control’s property window or If you need to add the items at run-time there are several ways to add a new item in the list box such as the ‘add’ method to add items of one-to-one items, ‘AddRange’ method to add the items from an array of items and if you want to insert an item at a specific location within the list item collection, you can use the Insert method.

Add single item into the list box

The following example shows how to create a list box control and then add items one by one at runtime:

Private Sub Form1_Load(sender As Object, e As System.EventArgs)
    Dim ListBox1 As New ListBox()
    ListBox1.Size = New System.Drawing.Size(200, 100)
    ListBox1.Location = New System.Drawing.Point(10, 10)
    ' Add the ListBox to the form.
    Me.Controls.Add(ListBox1)
    ' Set the ListBox to display items in multiple columns.
    ListBox1.MultiColumn = False
 
	ListBox1.Items.Add("NewDelhi")
	ListBox1.Items.Add("Londan")
	ListBox1.Items.Add("Kolkata")
End Sub

If the Sorted property of the ListBox is set to true, the item is inserted into the list alphabetically. Otherwise, the item is inserted at the end of the list.

Add items into the list box from an Array

You can add an array of items to the list of items for a System.Windows.Forms.ListBox with the help of AddRange() method.

Private Sub Form1_Load(sender As Object, e As System.EventArgs)
    Dim ListBox1 As New ListBox()
    ListBox1.Size = New System.Drawing.Size(200, 100)
    ListBox1.Location = New System.Drawing.Point(10, 10)
    ' Add the ListBox to the form.
    Me.Controls.Add(ListBox1)
    ' Set the ListBox to display items in multiple columns.
    ListBox1.MultiColumn = False
 
    Dim strArr() As String = {"NewDelhi", "Londan", "Kolkata", "Sydney", "NewYork"}
    ListBox1.Items.AddRange(strArr)
End Sub

Insert an item into the list box at the specified index.

‘Insert(int index,object item)’ method can be use to insert item at specified zero based index. Suppose in above example if you want to add a new city at the 3rd position you can write the code as:

 ListBox1.Items.Insert("Tokyo",2)

You can also manipulate the items of a ListBox by using the DataSource property. If you use the DataSource property to add items to a ListBox, you can view the items in the ListBox using the Items property but you cannot add or remove items from the list using the methods of the ListBox.ObjectCollection.

Remove item from Listbox

Remove single item
You can use ‘Remove(item as object)’ method to remove any item from the list box. After removing an item from the list, the indexes change for subsequent items in the list so ‘IndexOf(item as object)’ method for the particular item can give the different value after removing an item.

ListBox1.Items.Remove("Londan")

Remove item at the specified index from the list box
You can remove the item at the specified index within the collection with the help of ‘RemoveAt(index as integer)’ method. Suppose if you want to delete third item ‘Tokyo’ from the list on the click of button named ‘Button1’ then you can do that with the help of ‘RemoveAt(index as integer)’ function.

Private Sub Form1_Load(sender As Object, e As System.EventArgs)
	ListBox1.Items.Add("NewDelhi")
	ListBox1.Items.Add("Londan")
	ListBox1.Items.Add("Tokyo")
End Sub
 
Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
// "Tokyo" will be removed from the list box
ListBox1.Items.RemoveAt(2)
End Sub

Remove all items from the list box
you can clear all items from list using the ‘Clear()’ method.

ListBox1.Items.Clear()