a listbox control displays a list of items and allows the user to select one or more drag from...
DESCRIPTION
The Items property holds an entire list of values from which the user may choose The list of values may be established at run time or as part of the form design To set list values in the form design: ◦ Select the list box in the Design window ◦ View properties & click the Items ellipsis button ◦ This property is a collection, a list of values ◦ Type each value on a separate lineTRANSCRIPT
List Boxes & More
The ListBox Control A ListBox control displays a list of items and allows
the user to select one or more Drag from Toolbox to create this control on a form
ListBox Items Property The Items property holds an entire list of
values from which the user may choose The list of values may be established at run
time or as part of the form design To set list values in the form design:
◦ Select the list box in the Design window◦ View properties & click the Items ellipsis button◦ This property is a collection, a list of values◦ Type each value on a separate line
ListBox Items.Count Property This property returns an integer with the
number of entries stored in the Items property
Example of use:
The number of entries in the list can be assigned to an integer variable
If lstEmployees.Items.Count = 0 ThenMessageBox.Show("The list has no items!")
End If
numEmployees = lstEmployees.Items.Count
Item Indexing The Items property values can be accessed
from your VB code Each item value is given a sequential index
◦ The first item has an index of 0◦ The second item has an index of 1, etc.
Example:
name = lstCustomers.Items(2)' Access the 3rd item value
Index Out of Range Error The index of the last item is always list.Items.Count-1
Reference to an index greater than Count-1 or less than zero throws an exception
An exception handler can trap this error The variable ex captures the exception
thrownTry
strInput = lstMonths.Items(n).ToString()Catch ex as Exception
MessageBox.show(ex.Message)End Try
ListBox SelectIndex Property
The SelectIndex property returns an integer with the index of the item selected by the user
If no item is selected, the value is set to -1 (an invalid index value)
Can use SelectIndex to determine if an item has been selected by comparing to -1
Example:If lstLocations.SelectedIndex <> -1 Then
location = lstLocations.Items(lstLocations.SelectedIndex)End If
ListBox SelectedItem Property Instead of using the SelectedIndex property
as follows:
The SelectedItem property can be used to retrieve the value of a selected item as follows:
If lstMonths.SelectedIndex <> -1 Thenmonth = lstMonths.Items(lstMonths.SelectedIndex)
End If
If lstMonths.SelectedIndex <> -1 Thenmonth = lstMonths.SelectedItem.ToString)
End If
ListBox Sorted Property Sorted is a boolean property When set to true, values in the Items
property are displayed in alphabetical order When set to false, values in the Items
property are displayed in the order they were added
ListBox Items.Add Method Items can be added to the end of a ListBox
list in your VB code using the Add method Format is ListBox.Items.Add(Item)
ListBox is the name of the control Item is a string value to add to the Items
property Example:
lstStudents.Items.Add("Sharon")
ListBox Items.Insert Method Items can be added at a specific position of
a ListBox in VB code using the Insert methodListBox.Items.Insert(Index, Item)
Index specifies position where Item is placed
Index is zero based similar to SelectedIndex property
Items that follow are “pushed” down Example inserting "Jean“ as the 3rd itemlstStudents.Items.Insert(2, "Jean")
ListBox Methods to Remove Items ListBox.Items.RemoveAt(Index)
◦ Removes item at the specified index ListBox.Items.Remove(Item)
◦ Removes item with value specified by Item ListBox.Items.Clear()
◦ Removes all items in the Items property Examples:
lstStudents.Items.RemoveAt(2) ‘remove 3rd itemlstStudents.Items.Remove(“Jean”) ‘remove item JeanlstStudents.Items.Clear() ‘remove all items
Other ListBox Methods ListBox.Items.Contains(Item)
◦ Returns true if Item is found in the collection ListBox.Items.IndexOf(Item)
◦ Returns an integer with the index position of the first occurrence of Item in the collection
Examples:blnFound = lstMonths.Items.Contains(“March”)intIndex = lstMonths.Items.IndexOf(“March”)
Slide 5- 14
List Box Multicolumn Property The ListBox has a Multicolumn property
◦ Boolean property with default value of false◦ If set to true, entries can appear side by side
Below, ColumnWidth is set to 30 Note the appearance of a horizontal scroll
bar in this case
Slide 5- 15
Checked List Box A form of ListBox with the list box properties
and methods already discussed One item at a time may be selected but
many items in a Checked List Box can be checked
The CheckOnClick property determines how items may be checked◦ False - user clicks item once
to select it, again to check it◦ True - user clicks item only once
to both select it and check it
Slide 5- 16
Finding the Status of Checked Items
The GetItemChecked method returns true if the item at Index has been checkedCheckedListBox.GetItemChecked(Index)
Dim i as IntegerDim intCheckedCities as Integer = 0
For i = 0 to clbCities.Items.Count – 1If clbCities.GetItemChecked(i) = True Then
intCheckedCities += 1End If
Next i
MessageBox.Show(“You checked “ & _intCheckedCities.Tostring() & “ cities.”)
Slide 5- 17
Combo Boxes Similar to List Boxes Both display a list of items to the user Both have Items, Items.Count,
SelectedIndex, SelectedItem, and Sorted properties
Both have Items.Add, Items.Clear, Items.Remove, and Items.RemoveAt methods
These properties and methods work the same with combo boxes and list boxes
Slide 5- 18
Additional Combo Box Features A combo box also functions like a text box The user may enter text into a combo box Or the user may select the text from a
series of list box type choices
Slide 5- 19
Choosing a Combo Box Style If restricting the user to select items listed
◦ If empty space – use ListBox◦ If limited space – use drop-down list ComboBox
If allowing user to select an item listed or enter an entirely new item◦ If empty space – use simple ComboBox◦ If limited space – use drop-down ComboBox
Defensive Programming When you code, you need to anticipate
what the user is going to do with your code and what ways they can crash it
Some languages have error messages that you can capture◦ Once captured you can display to the user an
error message
Slide 5- 21
Examples of Input Validation Numbers are checked to ensure they are within
a range of possible values◦ For example, there are 168 hours in a week◦ A person can’t work more than 168 hours a
week Values are checked for their “reasonableness”
◦ A person might possibly work 168 hours in a week
◦ However, this is highly improbable Items selected from a menu or a set of choices
are checked to ensure these options are available
Variables are checked for values that might cause problems, such as division by zero
On the Lookout Empty input
◦ User hits enter before entering data How would we test for this?
Incorrect Datatype◦ User enters in a string for a number, etc.
Comparison against a list of acceptable values◦ State abbreviations◦ Zip codes
On the Lookout String length
◦ State abreviation = 2 character strings◦ How would we test for this?
Numbers in a reasonable range◦ Hourly wages, salary amounts, # of hours
Dates in reasonable ranges◦ There is no February 30th
Time measurements checked