list boxes

24

Upload: ailsa

Post on 04-Jan-2016

45 views

Category:

Documents


4 download

DESCRIPTION

List Boxes. List Box in Widget Swap. Drop Down Lists in GPE. Naming Convention. List boxeslst (El Es Tee – not 1 s t) Drop Down Listscbo (for combo box – old name) Orddl (whichever you use be consistent) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: List Boxes
Page 2: List Boxes

List Box in Widget Swap

Page 3: List Boxes

Drop Down Lists in GPE

Page 4: List Boxes

Naming Convention

List boxes lst (El Es Tee – not 1 s t)Drop Down Lists cbo (for combo box – old

name) Or ddl (whichever you use be

consistent)

All examples we shall relate to list boxes however the code also applies to drop down lists

Page 5: List Boxes

Things we want to do with List Boxes Add an item to the list

Set the entry so it makes sense to the user Set the entry so it makes sense to the system

Remove an item from the list Read the value of the item selected by the

user, i.e. find out what they clicked on Clear the list box Validate a list to make sure that an item has

been selected

Page 6: List Boxes

The Items CollectionHere we can see three items listed in this

list’s “items collection”

Page 7: List Boxes

Grouping data in ListsIn computer programming there are various

types of List structures that allow us to store more than one value in a singular structure.

Imagine a list of Strings or Integers or even Objects. (e.g. Employees)

Each item in a list has an Index. This is a numeric integer value that specifies the item’s position within the list. Indexing always starts at zero and ends at the list’s size – 1. In a list of 5 items, the last item will have an index of 4 and the first item an index of 0.

Page 8: List Boxes

ListBox - collection of ListItemsA ListBox will contain 0, 1 or more ListItem

objects.

The data within a ListBox web control is stored within a List collection that is referred to by Items. (e.g. lstToDo.Items)

Each ListItem has a Text and Value property. The Text is what you see on the screen and the Value is the SystemID.

Page 9: List Boxes

Accessing a ListBox itemTo access an individual Item within the ListBox

you will use the notation:- lstToDo.Items.Item(i) where i represents the index of the item you wish to access.

For example (set and get Text and Value properties):-lstToDo.Items.Item(0).Text = “Blue Widget”lstToDo.Items.Item(0).Value = “bluewidget.jpg”

Widget = lstToDo.Items.Item(2).TextWidgetImage = lstToDo.Items.Item(2).Value

Page 10: List Boxes

Identifying What the User SelectstxtSelectedItem.Text =

lstToDo.SelectedValue

Returns the value…

“Ring John”

Page 11: List Boxes

ListBox PropertieslstNames.SelectedItem.Text This allows us to obtain the text of

the item in the list that has been selected by the user.

lstNames.SelectedValue Contains the system ID of the selected item in the list. (If you do not set this yourself it will be the same as SelectedItem.Text)

lstNames.SelectedIndex This tells us the number of the selected item in the list. (If this is -1 it means that no selection has been made.) Each item in a list box has an index. The first item has an index of zero, the second one and so on.

Page 12: List Boxes

ListItemCollection PropertieslstNames.Items.Count The items have a count

property that tells us how many items are in the list.

lstNames.Items.Item(2).Text

lstNames.Items.Item(4).Value

Allows us to get or set the Text or system ID Value of an item in the list.

• Note - Items refers to the List collection contained within the List Box

- Item(i) refers to the list item at index i, where i is an integer within the range of the list (0 . . n-1) where n represents the number of items in the list.

Page 13: List Boxes

ListItemCollection MethodslstNames.Items.Add(MyName)This method allows us to add an item to a list box.

lstNames.Items.RemoveAt(3)Allows us to remove an item from a list box.

lstNames.Items.Clear()Clears the items collection of all entries.

Page 14: List Boxes

Adding Items to a List BoxIn the following example we will type two values.

Breakfast this is the value that will appear to the user

a this is the identifier for this entry

Page 15: List Boxes

Once the Item is Added…

Page 16: List Boxes

Add Some More Items… Lunch b Supper c

Page 17: List Boxes

Adding Items

Page 18: List Boxes

Setting the Item’s Value Two things to consider…

The text (that the user sees) The value (that the system uses)

If we have a list with three items it has the following properties...

Index

Items.Count = 3 Breakfast 0

Lunch 1

Supper 2

Page 19: List Boxes

Consider the Following Code…

Page 20: List Boxes

Identifying what the User SelectsThe SelectedValue property contains the

identifier of the item (the value) that the user has selected

If we do not explicitly set the value of an item ourselves, the SelectedValue contains the text of the entry in the list

Page 21: List Boxes

Deleting Items on the List

Page 22: List Boxes

Ensuring that a User has Selected an Item

Page 23: List Boxes

Clearing the List BoxShould you wish to clear the entire contents

of the list box, you would use the Clear method

For example, the following code would clear a list box called lstListExample

lstListExample.Items.Clear

Page 24: List Boxes

Questions1. Write a section of code that adds the days of

the week to the list box lstDays. (Sun, Mon, Tue, Wed, Thu, Fri, Sat)

 2. Write a line of code that removes “Wed”

from the list box. 3. Write a section of code that validates if an

item has been selected in the list box lstRoomForBooking