Lesson IX
Lesson IX
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.
Property Description
BackColor Specifies the ListBox’s background color
Columns 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.
IntegralHeight 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
List Holds the items in your ListBox.
MultiSelect The state of the ListBox’s 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 1-Simple, 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 Shift-click 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.
Sorted 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.
Style 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.
46
The following are the methods supported by ListBoxes. These methods help the user initialize, add items to,
and remove items from a ListBox.
Method Description
AddItem Adds a single item to the ListBox
Clear Removes all items from the ListBox
List A string array that holds items from within the ListBox.
ListCount The total number of ListBox items.
RemoveItem Removes a single item from the ListBox.
where ListCourses is the name of the ListBox and “Computer Science” is the item to be added.
ListCourses.RemoveItem(0)
ListCourses.Clear
IN FOCUS: COMBOBOX
ComboBoxes work much like ListBoxes except that these may allow the user to add items to a ComboBox at
runtime through a built-in TextBox. VB has three kinds of ComboBoxes. All the properties of a ListBox apply
to a ComboBox.
Kind Description
Drop-down ComboBox 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.
Simple ComboBox 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.
Drop-down ListBox Do not allow you to enter values, so it is similar to
a ListBox. It looks like a Drop-down ComboBox.
47
Use the Style property to switch from one kind of ComboBox to another. The Form below displays the three
kinds.
48
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 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.
Extra Toppings:
Cheese P 5.00
Ham P 15.00
Onions P 8.00
Pepper P 10.00
49