Visual Basic Tutorials – Controls – The ListBox
Visual Basic Controls – The ListBox
The ListBox control occupies a user-specified amount of space on the form and is populated with a list of items. If the list of items is longer than can fit on the control, a vertical scroll bar appears automatically.
Figure shows a listbox control as displayed in the tool box of Visual Basic. To place the list box in the form, click on the symbol shown in Figure and then click – and drag in the form where the control is to be placed.
The advantage of using a list box lies on the user not having to remember all possible options in addition. It also precludes the possibility of an invalid option.
The ListBox Properties:
Some properties of listbox are:
List Index | Is a number used to access individual elements in the listbox. The list index starts with 0 for the item in the list box and takes on consecutive numbers for the following elements. Thus, the list index identifies each item in the list box. |
Sorted | Used to display the items in a sorted order. If this option is not set then the items appear in the order in which they have been added to the list. |
MultiSelect | Used to specify if the user can select multiple items in the list. |
ListCount | Used to return the number of items in a list box. |
Selected | Sets the selection status of an item in a ListBox control. |
The ListBox Method:
List box methods are used to manipulate the list box and/or its items at run-time. Here are some methods that are commonly used:
AddItem | This method adds the specified item to the list box. The new item is appended at the end of the list. |
RemoveItem | Used to delete an item from the list. |
SetFocus | Used to make the list box the current active element. |
The ListBox Events:
Some events commonly required of a list box events are:
Click | Occurs each time the user clicks on the listbox. |
Scroll | Occurs when a user scrolls theough the list in the list box |
Adding Items to a ListBox:
You can add items to a list box at either design time or at run-time. At design time, you can use the List property, which is a very handy array of the items in the list box; and at run-time, you can use both the List property and the AddItem() method. Here’s how you use the List property in code (keep in mind that you can get or set items in the list box with the List array):
ListBox
.List(index) [= string]
How do you keep track of the total number of items in a list box? You use the ListCount property; that is, if you loop over the List array, you’ll use ListCount as the maximum value to loop to. At design time, you can add items directly to your list box by typing them into the List property in the Properties window. Selecting the List property displays a drop-down list (which is appropriate considering you’re filling a list box), and you can type item after item into the list box that way.
At runtime, you can either use the indexed List property as detailed previously, or the AddItem() method this way:
Private Sub Form_Load()
List1.AddItem (“Item 1”)
List1.AddItem (“Item 2”)
List1.AddItem (“Item 3”)
List1.AddItem (“Item 4”)
End Sub
Removing item from ListBox
You can remove items from a list box at design time simply by deleting them in the List property. At runtime, you use the RemoveItem() method.
Item 0 has index 0 in the list box, Item 1 has index 1, and so on. To remove, say, Item 1 when the user clicks a command button, we can use RemoveItem and pass it the item’s index:
Private Sub Command1_Click()
List1.RemoveItem 1
End Sub
TIP: You should note that removing an item from a list box changes the indexes of the remaining items. After you remove Item 1 in the preceding example, Item 2 now gets index 1 and Item 3 gets index 2. If you want to change those indexes back to their original values, set the items Index properties.