Introduction
In this chapter, we will discuss about the List box
and Combo box controls. These controls are also called
multivalue controls.
List Box Controls
Here is a list of the different types of list boxes:
- The Simple list box
- The drop-down list box
- The drop-down combo box
- The simple combo box
Simple List Boxes
The simple list box gives the users a way to select
one or more items from a list of choices. You will
generally not initialize the list box controls list
of values from within the properties window. VB provied
the means to initialize the list at runtime.
A simple list box is a list box that displays
items from a list. |
|
The programmer initializes the
list, and the user cannot directly add or delete
items from the simple list box. |
You can use the AddItem
method to add items to a list. Consider the following
program segment, which adds values to the list box
in the Form_Load event:
1: Private Sub Form_Load()
2: lstNames.AddItem("Mark Waugh")
3: lstNames.AddItem("Ricky
Ponting")
4: lstNames.AddItem("Sachin
Tendulkar")
5: lstNames.AddItem("Shane
Bond")
6: End Sub
The Form_Load Event is the occurs when a form loads.
A form loads:
- When the application runs, if the form is a startup
form
- Via code by calling the Load statement for a form
- When any reference is made to an unloaded form's
properties or controls.
This event is a good place to load initial list values.
|
Once you add the initial items,
you can use the AddItem method to add more items
to the list box. |
VB adds items to the list in the same order your
code adds the items, unless you change the list box's
Sorter property to True. The following figure shows
a simple list box
Figure 12.1
List box items have index values, and you distinguish
each list box item from the others by its index. The
index starts from 0 for the first item and increases
without duplicates in the list. The list box's ListIndex
property holds the value of the currently selected
item.
|