a listbox control displays a list of items and allows the user to select one or more drag from...

23
List Boxes & More

Upload: basil-daniel

Post on 08-Jan-2018

214 views

Category:

Documents


0 download

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 line

TRANSCRIPT

Page 1: 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

List Boxes & More

Page 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

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

Page 3: 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

Page 4: 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.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

Page 5: 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

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

Page 6: 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

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

Page 7: 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 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

Page 8: 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 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

Page 9: 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 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

Page 10: 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.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")

Page 11: 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.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")

Page 12: 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 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

Page 13: 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

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”)

Page 14: 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

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

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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