C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

Deleting an item from List Box or Combo Box in VB


Tip Abstraction:
         You can simply remove item or elements from your visual basic listbox or combobox with various methods


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

Remove selected item from listbox combobox in VB

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 

Here we type a number of position in a text box and removal of item is based on this textbox text.

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

Remove listbox combobox item by its position in VB

Note that, never type invalid positions, that may get error like Argument Out Of Range Exception – Invalid argument value.


No comments:

Post a Comment