ListBox, ComboBox, CheckBox, OptionButtons, and TimerControl
ListBox, ComboBox, CheckBox, OptionButtons, and TimerControl
ListBoxes can be set up to allow user to select one item from the list, or to allow multiple selections. If your
list is too long for the size you’ve drawn the ListBox, scroll bars will appear automatically. Each item in a
ListBox has a corresponding index, and you can refer to the item in the ListBox using this index. When the
user selects (click) a list item, it is highlighted and the ListBox’s ListIndex property is automatically changed
to hold a value corresponding to the item. The first item in the list gets the index 0, the next index 1, and so
on. Use the ListIndex property by itself to determine what the user selected, or use it in combination with the
List property.
Just remember that the ListIndex property is the number of the selected item and the List property is the
text.
You use two main events with listbox: Click and DblClick. How you actually use them is up to you, because
different programs have different needs. You use Click event just as you use the Click event in a button,
with a Click event handler. In this example, we display the item in the listbox that the user has clicked, using
the ListIndex property.
And displaying the selected item is the same for DblClick you just add a DblClick handler with the code you
want:
TEXT PROPERTY
An easier way to get the selected item is to use the Text property. For example:
lstChoices.Text
lstChoices.List(lstChoices.ListIndex)
When no item is selected, the value of ListIndex will equal to -1. You could use that in an If statement to run
code which required a selected item.
If lstChoices.ListIndex = -1 Then
Msgbox “No item selected.”
Else
Msgbox “You selected “ & lstChoices.Text
End If
You can add items to a list box at design time using the List property. To do this, select the ListBox control
on the form; on the properties window, choose List property and type the item you want to include in the List
(press CTRL + Enter to add another item in the list).
To add item during execution, you can use the AddItem method. This method has one required argument
and one optional argument. For example:
lstChoices.AddItem “Java”
lstChoices.AddItem txtInput.Text
lstChoices.AddItem “Visual Basic”, 2
The line – lstChoices.AddItem “Java” adds the Text – Java at the end of the list. The next line
which is lstChoices.AddItem txtInput.Text adds to the end of the list what the user has
entered in a TextBox named txtInput (You’d probably use a Command Button and put the AddItem
code in the button’s Click event. The last line, lstChoices.AddItem “Visual Basic”, 2 adds
the text Visual Basic to the list and uses an optional argument to give the item a certain place within
the list at the same time. This value would become the new item’s ListIndex and all items already in the
list with this ListIndex or higher would be pushed down one in the list and automatically given a new
ListIndex, obviously one higher.
You can delete items in a ListBox using the RemoveItem method. The RemoveItem method deletes a list
item; its one argument is the ListIndex of the item you wish to remove. Important to consider when
removing list items is that the ListIndex of the remaining items will change to reflect their new positions in
the list. For example, if you want to delete an item that is being selected by the user at Click event of a
command button named cmdDelete:
The Clear method deletes the entire list at once and requires no arguments.
You can alphabetize the items in a listbox by setting its Sorted property to True. You should know,
however, that sorting a listbox can change the indexes of the items in that listbox. After the sorting is
finished, the first item in the newly sorted list has index 0, the next index 1, and so on.
You can use the ListCount property to determine how many items are in a listbox.
A multiselect listbox allows the user to select a number of items at one time. You make a listbox into a
multiselect listbox with the MultiSelect property. The user can then select multiple items using the Shift and
Ctrl keys. Here are the possible settings for MultiSelect:
Let’s see an example of a multiselect listbox at work. In this case, we’ll have two listboxes named list1
and list2, as well as a command button named cmdAdd with caption - Add. Set list1’s MultiSelect
property to 2 – Extended. When the user selects a number of items in list1 and clicks the button
Add, we’ll copy the selected items in list1 to list2. As in this figure:
Option Explicit
Dim i As Integer
Most of what you already know about ListBoxes will apply to a ComboBox. Items are added using the
AddItem method, removing an item uses the RemoveItem method, and to clear a ComboBox, you have
to use the Clear method. List, ListIndex, and ListCount properties will also work the same way,
however, ComboBox cannot handle multiple selections.
ComboBoxes have Style property which can only be change at design time. That means you must decide
which style to use at design time before finally displaying it on the form during execution. There are three (3)
styles to consider:
1. The Dropdown Combo is probably the most used style. This style can be drawn as wide as it
needs to be but it’s height is limited to the space needed for one line of text (this is governed by the
Font Size). Using this style, the user will either select an item from the drop-down list or type in
their own.
2. The Simple Combo appears like you’ve drawn a TextBox just over a ListBox. If the user highlights
(clicks) one of the items in the ListBox portion, the text automatically appears in the TextBox
portion. This is the only style of ComboBox which will respond to a double click and the double
click must come from the ListBox portion of the control.
3. The Dropdown List style does not take user input. This style works identically to the Dropdown
Combo.
CheckBoxes and Option button have common properties. When interacting with CheckBoxes and Option
button, you have to use the Value property to check if the CheckBox has been checked or not, or if an
Option Button has been selected or not.
The CheckBox has the following option for its Value property:
0 – Unchecked
1 – Checked
2 – Grayed
The Value property for an Option button is a Boolean property – True or False.
Consider the following example, which displays a Message box when the user selects either the Male or
Female checkbox.
You use a Timer control when you want to execute code at specific intervals. To use a timer, you add a timer
control to your program and set its Interval property. From then on, while the timer is enabled, it creates
Timer events, which are handled in an event handling procedure, like Timer1_Timer(). You place the code
you want executed each interval in that procedure.
In using Timer in Visual Basic, you need to interact with the following Timer properties:
When you place a timer in your program, you can set its Enabled property to False, which means no Timer
events will occur. When you want to start the timer, you can set Enabled to True.
The main event for timers is the Timer event, and double-clicking a timer at design time creates a handler
function for that event:
Sub Timer1_Timer()
End Sub
All you need to do is to add the code you want executed to this procedure. For example, here we display the
current time in a label named lblDisplay using the Visual Basic Time function:
Sub Timer1_Timer()
lblDisplay.Caption = Time
End Sub