list boxes n.
Skip this Video
Loading SlideShow in 5 Seconds..
List Boxes PowerPoint Presentation

List Boxes

201 Vues Download Presentation
Télécharger la présentation

List Boxes

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. List Boxes

  2. List Box in Widget Swap

  3. Drop Down Lists in GPE

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

  5. 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

  6. The Items Collection • Here we can see three items listed in this list’s “items collection”

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

  8. 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.

  9. Accessing a ListBox item • To access an individual Item within the ListBox you will use the notation:- lstToDo.Items.Item(i) wherei 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

  10. Identifying What the User Selects txtSelectedItem.Text = lstToDo.SelectedValue Returns the value… “Ring John”

  11. ListBox Properties lstNames.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.

  12. ListItemCollection Properties lstNames.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.

  13. 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.

  14. Adding Items to a List Box • In 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

  15. Once the Item is Added…

  16. Add Some More Items… • Lunch b • Supper c

  17. Adding Items

  18. 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...

  19. Consider the Following Code…

  20. 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

  21. Deleting Items on the List

  22. Ensuring that a User has Selected an Item

  23. 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

  24. 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