ListBox control displays a list of items from which the user can select one or more items. If the total number of items exceeds, a scroll bar is automatically added to the ListBox control …….
Advantage of ListBox
- user did not have to remember all possible options.
- It also prevents wrong entry because the user did not have to type.
Using items property you can add or remove items in the ListBox. The first item will have index 0, the next item will have index 1, and so on in the ListBox. The items in the list boxes are stored in the Items collection.
To add items at run time, you can use Items.Add OR Items.Insert OR Items.AddRange method.
At run time you can use add method, to insert an item into the ListBox. For Example, ListBox1.Items.Add(“Boll Pen”)
you can use the Insert method, to inserts an item into the list box at the specified index. ListBox1.Items.Insert(index As Integer, Item As Object) For Example, ListBox1.Items.Insert(3, “Eraser”)
you can also use AddRange method, to add a set of items to the list box in a single operation. For Example,
Dim DataArray(20) As String ……. ……… ListBox1.Items.AddRange(DataArraty)
To remove items at run time, you can use Items. Remove OR Items.RemoveAt method. At run time you can use the remove method, to remove a specific item from the ListBox.
For example to remove a currently selected item
ListBox1.Items.Remove(ListBox1.SelectedItem) You can also remove an item by passing corresponding object to remove. ListBox1.Items.Remove(“Boll Pen”)
you can use the RemoveAt method to delete items from a ListBox. For example, to remove the item at index 5 ListBox1.Items.RemoveAt(5)
Or, you can also remove the currently selected item with RemoveAt ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
- To remove all the items at run time, you can use Clear methods of the Items collection to clear a ListBox. For example, ListBox1.Items.Clear()
- To count the total numbers of items in the list box at run time, you can use the Count property of the Items collection. For example, Msgbox(ListBox1.Items.Count)
1. MultiColumn : Gets or sets a value indicating whether the ListBox supports multiple columns.
2. ScrollAlwaysVisible : When ScrollAlwaysVisible is set to true, the scroll bar appears regardless of the number of items.
3. Sorted : Gets or sets a value indicating whether the items in the list box are sorted alphabetically.
4. SelectionMode : How many items can be selected at a time and how the user can make multiple selections. You can set this property to None, One, Multisimple, or MultiExtended.
- None :- No items can be selected.
- One :- Only one item can be selected. (By Default)
- MultiExtended :- Multiple items can be selected, and the user can use the SHIFT, CTRL, Arrow keys, and mouse click to make selections.
- MultiSimple :- Multiple items can be selected, and the user can use mouse click or pressing the SPACEBAR selects or deselects an item in the list.
5. Items : Gets the items of the list box.
6. SelectedItem (Only available at run time, Dynamic property) : This property represents the item that is currently selected in the Listbox.
7. SelectedItems (Only available at run time, Dynamic property) : This property represents a collection of all the items that are currently selected in the ListBox.
8. SelectedIndex (Only available at run time, Dynamic property) : This property represents the index of the currently selected item in the ListBox. If no item is selected in the Listbox, then SelectedIndex property will return -1.
9. SelectedIndices (Only available at run time, Dynamic property) : This property represents a collection of all the indexes that are currently selected in the ListBox.
- ClearSelected :- Unselects all items in the ListBox.
- FindString :- Finds the first item in the ListBox that starts with the string specified as an argument.
- FindStringExact :- Finds the first item in the ListBox that exactly matches the specified string.
SelectedIndexChanged :- Occurs when the SelectedIndex property of a list box is changed.