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.

 

 

 

 

 


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.

 

 

 

 

 

 

 

 

Types of ListBoxes: (Left) Standard ListBox; (Right) CheckBox ListBox

 
 

 


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

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.

 

 

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.

 

 

How do I add an item to a ListBox?

There are two ways.  One is by entering items in the List property of a ListBox.   The other way is by executing the statement:

 

                        ListCourses.AddItem “Computer Science”

 

where ListCourses is the name of the ListBox and “Computer Science” is the item to be added.

 

How do I remove an item to a ListBox?

Each item in a ListBox is associated with a subscript or index.  The first item has a subscript 0, while the nth has subscript n-1.  Execute the following to remove the first item.

 

                        ListCourses.RemoveItem(0)

 

How do I remove all items in a ListBox?

Just execute the statement:

 

ListCourses.Clear

 

How do I know how many items are there in a ListBox?

The expression ListCourses.ListCount returns the total number of items in the ListBox named ListCourses.

 

How do I know if an item is selected or not?

Use the Selected method. If you want to know if the first item is selected, the expression ListCourses.Selected(0) returns True if item 0 is selected; otherwise, it returns False.


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. 

 

Here are the three kinds of ComboBoxes:

 

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.

 

Use the Style property to switch from one kind of ComboBox to another.  The Form below displays the three kinds.


 

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

 

 


 

 

 


Instructions: Make a program that computes for a pizza’s 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              P 5.00

                        Ham                  P 15.00

                        Onions              P 8.00

                        Pepper              P 10.00

 

            Base your program design on the following Form.

 

 

 

 

 

 

 

 

 

 

 

 

Save your work as Lesson9.vbp.

 

 

[ Home ]