![]() ![]() | ||
The Testing Department is calling again-how about letting the users customize your program? You ask: what do you mean? Well, they say, let's give the user some way of removing the 50 fine French cooking tips from the list box.
You can use the RemoveAt method to delete items from a list box. To remove the item at index 5, you'd use this code:
ListBox1.Items.RemoveAt(5)
Here's how you'd remove the currently selected item with RemoveAt:
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
You also can use the Remove method to remove a specific object from a list box. Here's how I'd remove the currently selected item with Remove:
You also can remove items by passing the corresponding object to Remove. For example, if I've filled a list box with String objects, I can remove the item "Item 1" this way:
ListBox1.Items.Remove ("Item 1")
Tip |
You should note that removing an item from a list box changes the indices of the remaining items. After you remove item 1 in the above example, item 2 now gets index 1, and item 3 gets index 2, and so on. |
You also can use the Items.Clear method to remove all items from the list box.
![]() ![]() | ||