Lesson Ix.: Listbox and Combobox
Lesson Ix.: Listbox and Combobox
Objectives
To create selection lists at design time using ListBox and ComboBox
To be able to differentiate ListBox from a ComboBox
To be able to add/remove an item from a ListBox or ComboBox at runtime.
Notes
IN FOCUS: LISTBOX
ListBox gives the user a choice of several values. The user selects an option instead of typing a
value into a Textbox. The ListBox ensures that the user always chooses one of the available
options.
As in the figure above, the ListBox displays scrollbars if it is not tall enough or wide enough to
display all its data. The contents of the ListBox may be set at design time or at runtime.
The following are the significant properties of a ListBox.
Property
BackColor
Columns
IntegralHeig
ht
List
MultiSelect
Sorted
Style
Description
Specifies the ListBoxs background color
Determines the number of columns. If 0, the ListBox scrolls
vertically in a single column. If 1 or more, the ListBox items
appear in the number of columns specified (one or more
columns) and a horizontal scrollbar appears so you can see
all the items in the list.
Boolean. Determines whether the ListBox can display partial
items, such as the upper half of an item that falls toward the
bottom of the ListBox. True (default): Does not display partial
items
Holds the items in your ListBox.
The state of the ListBoxs selection rules. If 0-None (the
default), the user can select only one item by clicking with
the mouse or by pressing the spacebar over an item. If 1Simple, the user can select more than 1 item by clicking with
the mouse or by pressing the spacebar over items in the list.
If 2-Extended, the user can select multiple items using Shiftclick and Shift-arrow to extend the selection from a
previously selected item to the current one. Control-click
either selects or deselects an item from the list.
Determines whether the ListBox values are automatically
sorted. If False (the default value), the values appear in the
same order in which the program added the items to the list.
Determines whether the list box appears in its usual list
format or, as in the figure in the previous page, with the
Checkbox before the items.
The following are the methods supported by ListBoxes. These methods help the user initialize, add
items to, and remove items from a ListBox.
Method
AddItem
Clear
List
ListCount
RemoveIte
m
Description
Adds a single item to the ListBox
Removes all items from the ListBox
A string array that holds items from within the ListBox.
The total number of ListBox items.
Removes a single item from the ListBox.
Simple ComboBox
Drop-down ListBox
Description
Displays only one item unless the user
clicks the button to display additional
items (a scrollbar appears if there are more
items than what the ComboBox can
display). The user can also enter values at
the top of the ComboBox in the same way
you do in a TextBox.
Looks like a ListBox attached to a TextBox.
Items are displayed as if they were in a
ListBox. You may also enter values on top
of the ComboBox.
Do not allow you to enter values, so it is
similar to a ListBox. It looks like a Dropdown ComboBox.
Use the Style property to switch from one kind of ComboBox to another. The Form below displays
the three kinds.
Lesson in Action
Let us make an application that formats the font style of a Label. A ListBox will provide a list of
available font names.
The application will look like this:
The ListBox contains the following font names: Arial, Century Gothic, Times New Roman, and
Tahoma. You may add several others.
Now when we select a font from the ones in the ListBox, the Label should automatically be
formatted. Thus, the main event will be a click on the ListBox.
The code is a short one. We just need to assign the Text property of the currently selected ListBox
item to the FontName property of the Label.
Private Sub lstFonts_Click()
lblBanner.FontName = lstFonts.Text
End Sub
On your Own
Instructions: Make a program that computes for a pizzas price based on the size and toppings
selected.
Size:
Small
P 40.00
Medium
P 75.00
Family
P 100.00
Large
P 140.00
Extra Toppings:
Cheese
Ham
Onions
Pepper
P 5.00
P 15.00
P 8.00
P 10.00