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) 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 Collection Here we can see three items listed in this lists items collection
Grouping data in Lists In 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 items position within the list. Indexing always starts at zero and ends at the lists 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 ListItems A 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 item To 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).Text WidgetImage = lstToDo.Items.Item(2).Value
Identifying What the User Selects txtSelectedItem.Text = lstToDo.SelectedValue Returns the value… Ring John
ListBox Properties lstNames.SelectedItem.TextThis allows us to obtain the text of the item in the list that has been selected by the user. lstNames.SelectedValueContains 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.SelectedIndexThis 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 Properties lstNames.Items.CountThe 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 Methods lstNames.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 Box In the following example we will type two values. Breakfastthis is the value that will appear to the user athis is the identifier for this entry
Setting the Items 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 = 3Breakfast0 Lunch1 Supper2
Identifying what the User Selects The 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
Clearing the List Box Should 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
Questions 1. 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