List Box in Widget Swap Drop Down Lists in GPE Naming Convention List boxeslst (El Es Tee – not 1...

Post on 30-Mar-2015

215 views 0 download

Tags:

transcript

List Box in Widget Swap

Drop Down Lists in GPE

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

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

The Items CollectionHere we can see three items listed in this

list’s “items collection”

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.

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.

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

Identifying What the User SelectstxtSelectedItem.Text =

lstToDo.SelectedValue

Returns the value…

“Ring John”

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.

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.

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.

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

Once the Item is Added…

Add Some More Items… Lunch b Supper c

Adding Items

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

Consider the Following Code…

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

Deleting Items on the List

Ensuring that a User has Selected an Item

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

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