Visual Basic Tutorial
The
Listbox Control
Objectives
This guide will help
you to: -
- Create a listbox.
- Add and Remove items to a listbox.
- Create a simple listbox and label application.
Introduction
Creating
a Listbox
Adding Items - List Property
AddItem Method
RemoveItem Method
A Simple Program
Helpdesk
Exercise
List boxes are used to supply a list of options to
the user. The picture below shows a listbox.

A list box is used when the user is to be presented with a fixed set of
choices, to be made only from the list of items displayed.
Examples might be a list of available holiday destinations or a
list of courses available from a college, or a menu list at a restaurant.
In Visual Basic, to create a list box, double click on the toolbox icon
in the toolbox.
You can drag the resulting box into place on the form and resize it
if you wish.
There are two ways of adding items to your listbox. The first
is by using the List property in the Properties
window. The second is by using code.
To add items to the listbox using the List
property, locate the List property in the Properties
window.

|
If you click on the little arrow button you can type in the
items you want in your list.
As you can see, I have started typing in the months of the
year.
|
|
|
Here is a picture of my listbox after adding all the months
of the year.
You will notice that vertical scroll bars are added automatically
if the list box is not deep enough to display all the items.
|
|
To add items to a listbox using code you can use
the AddItem method
|
|
Private Sub Form_Load()
List1.AddItem "January"
List1.AddItem "February"
List1.AddItem "March"
List1.AddItem "April"
List1.AddItem "May"
List1.AddItem "June"
List1.AddItem "July"
End Sub
|
Why don't you try adding the rest of the months using the AddItem
method?
|
  
Note:
List items appear in the order they were typed. If you change the sort
order to true in the Properties window by then the
list will be sorted into alphabetical order.
|
To remove items from a listbox using code you
can use the RemoveItem method
|
|
Private Sub Form_Load()
//add the following code after code you typed in
before List1.RemoveItem 0
List1.RemoveItem 2
End Sub
|
Which items do you think will be removed from the list?
Read the note below before making up your mind.
|
  
Note:
List items have a property called a ListIndex. This means
that the first item in the list has a ListIndex of 0, the second
item has a ListIndex of 1, and so on.
So,
in my list, the item with the ListIndex of 0 is
'January'. The item with the ListIndex of 2 is
'March'.
|
To add an item to the list in a particular order just specify
the ListIndex number. I have added 'March' back into the
list using the code below.
|
|
Private Sub Form_Load()
//add the following code after code you typed in
before List1.AddItem "January", 0
List1.AddItem "March", 2
End Sub
|
Now, let's suppose I want something to happen when the user
clicks on a list item. Let's add a label below the listbox that displays some
sort of informational message when the user clicks on an item in the listbox.
Add a label to your form.
Now double click the listbox. This should take you to
the coding window where you should see this:
|
|
Private Sub List1_Click()
End Sub
|
Type in the following code:-
|
|
Private Sub List1_Click()
If (List1.ListIndex = 0) Then
Label1.Caption = "January is a cold
month"
End If
If (List1.ListIndex = 1) Then
Label1.Caption = "It sometimes snows in
February"
End If
End Sub
|
|
When you run your program you
should see something similar to the picture:-
The label caption should change when you click on January
and change again when you click on February.
It will not change when you click on any of the other months
unless you add extra code. |
|
The property ListIndex gives the index of the currently selected list
item. So the statement...
If (List1.ListIndex = 0)
...is a way of seeing if the currently selected list item has
a ListIndex of 0.
It is your turn to create a listbox
application.
~Try the activity~
|
Activity
A |
Create a simple program that displays a list of common computer
problems in a listbox. Possible solutions to the
problems in the list should be displayed in a label below the listbox. See the picture below:-
|

Fini
|