0% found this document useful (0 votes)
36 views

Lesson Ix.: Listbox and Combobox

This document discusses ListBox and ComboBox controls in Visual Basic. It describes the properties and methods of ListBoxes, including how to add, remove, and select items. ComboBoxes are similar but also allow entry of new text. The document provides an example of an application that uses a ListBox to set the font of a label when an item is selected. Students are instructed to create a pizza ordering application using ListBoxes to select size and toppings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lesson Ix.: Listbox and Combobox

This document discusses ListBox and ComboBox controls in Visual Basic. It describes the properties and methods of ListBoxes, including how to add, remove, and select items. ComboBoxes are similar but also allow entry of new text. The document provides an example of an application that uses a ListBox to set the font of a label when an item is selected. Students are instructed to create a pizza ordering application using ListBoxes to select size and toppings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

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
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.

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.
is by executing the statement:

The other way

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
Drop-down ComboBox

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

Base your program design on the following Form.

Save your work as Lesson9.vbp.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy