Download presentation
Presentation is loading. Please wait.
Published byBasil Daniel Modified over 9 years ago
2
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
3
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
4
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 Then MessageBox.Show("The list has no items!") End If numEmployees = lstEmployees.Items.Count
5
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
6
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 thrown Try strInput = lstMonths.Items(n).ToString() Catch ex as Exception MessageBox.show(ex.Message) End Try
7
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
8
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 Then month = lstMonths.Items(lstMonths.SelectedIndex) End If If lstMonths.SelectedIndex <> -1 Then month = lstMonths.SelectedItem.ToString) End If
9
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
10
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")
11
Items can be added at a specific position of a ListBox in VB code using the Insert method ListBox.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 3 rd item lstStudents.Items.Insert(2, "Jean")
12
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 3 rd item lstStudents.Items.Remove(“Jean”)‘remove item Jean lstStudents.Items.Clear()‘remove all items
13
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”)
14
Slide 5- 14 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
15
Slide 5- 15 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
16
Slide 5- 16 The GetItemChecked method returns true if the item at Index has been checked CheckedListBox.GetItemChecked(Index) Dim i as Integer Dim intCheckedCities as Integer = 0 For i = 0 to clbCities.Items.Count – 1 If clbCities.GetItemChecked(i) = True Then intCheckedCities += 1 End If Next i MessageBox.Show(“You checked “ & _ intCheckedCities.Tostring() & “ cities.”)
17
Slide 5- 17 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
18
Slide 5- 18 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
19
Slide 5- 19 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
20
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
21
Slide 5- 21 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
22
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
23
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 30 th Time measurements checked
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.