Table of Contents
Deleting an item from List Box or Combo Box in VB
It is very simple to add items to a list box or combo box with different ways. But how can we remove an item.? Removing an item from a list box or combo box is not complex. You can simply remove items with various methods
- Removing Selected Item
- Removing item with Index
- Removing item by item’s position
Removing Selected Item from a list box or comb box
Here List Box or Combo Box item is removed based on user’s selection. A button click event perform the removal action.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
ComboBox1.Items.Remove(ComboBox1.SelectedItem)
End Sub
You must select an item to perform delete action and here both List Box and Combo Box coding is inside a same event.
Removing List Box or Combo Box items by it’s Index
Index is a number which denote list box or combo Box items internally. In programming, all index are start from 0. That is the index of first item is 0, index of second item is 1 and so on.
Item index = item position – 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Items.RemoveAt(1)
ListBox1.Items.RemoveAt(1)
End Sub
Removing List Box or Combo Box items based on position:
In above method, we have removed items based on its index. The position of item depends its index
Item Position = Item Index + 1
That is index = position – 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ComboBox1.Items.RemoveAt(1)
‘Removes 2rd item from combo box
ListBox1.Items.RemoveAt(3)
‘Removes 5rd item from the list box
End Sub
Removing item by a position that typed in a textbox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim positon As Integer
positon = Val(TextBox1.Text)
ListBox1.Items.RemoveAt(positon – 1)
ComboBox1.Items.RemoveAt(positon – 1)
End Sub
Note that, never type invalid positions, that may get error like Argument Out Of Range Exception – Invalid argument value.
Be First to Comment