About 18 controls are available from the Tool Box at the left of the VB
window, and about 8 of these are explained in other projects that you have
already seen. One of those controls was the textbox control, which is used for
entering text. This brief and simple project is designed to introduce 2 other
controls, the listbox and combobox controls, which are good alternatives to the
textbox control if the user will often or always be selecting from a limited
number of text options. Not only do these controls mean that the user does
not have to do so much typing, but also they allow us to restrict the user's
choices when necessary. For example, if we ask several students to type
the name of their class, students in the same class might type 3°2, 3.2, 3eme
2, troisième 2 etc. and it might then be difficult for the program code to recognize
that these are in fact the same class. Using a listbox containing a list of
possible classes, this is no longer a problem.List Box
If the user will always be selecting text from a limited number of
options, use a listbox .
To learn how to use a listbox, let's assume we want the user to indicate on
which day of the week they were born. Make a new Standard EXE project and add a
listbox control to the form. Rename the listbox lst1, so as to conform to the
useful convention of using a 3 letter prefix for each control name. Set up the 7
days of the week by typing them into the List property of the listbox, using
Ctrl-Enter to move to each new line, and Enter when you have finished. Make the
listbox taller, until all 7 days are visible (if the listbox is not tall enough
to show all the items, then a scroll bar will appear, giving access to all the
items).
To see how you can use code to detect the user's choice, double-click the
control and add the following code:
Private Sub Lst1_Click()
msg = "There are " & Lst1.ListCount & " items in the list. "
msg = msg & "You chose item number " & Lst1.ListIndex
msg = msg & " which is " & Lst1.List(Lst1.ListIndex)
MsgBox (msg)
End Sub
Note how the code assembles a message into a variable called msg and
then displays the message in a message box. Running the program, you will notice
that the message indicates the number of items correctly, using the listcount
property. You may be surprised by the number of the item that you selected, but
that is only because the first item is item 0, not item 1. Note how the code to
retrieve the actual text that was selected is rather clumsy, for we need to use
first the listindex property to find which item was selected and then the list
property to display the corresponding text.
Combo Box
If the user will probably be selecting text from a limited number of
options, but may need to type in text that was not offered as an option, use a combobox
. The combobox combines the
convenience of the listbox with the freedom of the textbox.
Imagine that we want to ask the user to indicate his or her favourite sport,
it is likely that the user will choose football, baseball, tennis, rugby,
volleyball or swimming. However, some people may want to select something that
is not on the list, like squash, so a combobox rather than a listbox should be
used.
To practice working with a combobox, add one to your form and rename it cmb1.
Add the above sports to the combobox list property, using Ctrl-Enter to start
each new line and Enter to finish. Set the text property to "Choose a
sport".
It would be hard to code the combobox in the same way that the listbox was
coded for we would have to write code both for users who choose an item from the
list and also for users who choose instead to type into the combox. To
make things easier, add a commandbutton to the form, set its name to cmd1 and
its caption to 'Show combobox selection'. Set the command button code to:
Private Sub cmd1_Click()
msg = "There are " & cmb1.ListCount & " items in the list. "
msg = msg & "You chose item number " & cmb1.ListIndex
msg = msg & " which is " & cmb1.Text
MsgBox (msg)
End Sub
This is very similar to the code for the listbox, except that we have used
the text property of the combobox - this property does not exist for
listbox controls.
Now try running the program with different settings for the combobox style
property, noting that the simple combo setting is only useful if the
combobox has been stretched vertically.

Other features of both listbox controls and combobox controls:
 | the sorted property is often useful since it sorts the items of the
list property into alphabetical order. |
 | the multiselect property (listbox control only), when turned on,
allows the user to select multiple items simultaneously - check the help
system for more on this. |
 | it is also possible to use code to add or remove items from
the list property- check the help system to see how. |
For more information on the listbox and combobox controls, check the Visual
Basic help system. |