List Boxes Combo Boxes Provide a list for

List. Boxes & Combo. Boxes • Provide a list for the user to select from • Various styles, choose based on – Amount of data to be displayed – Space available – User's ability to add to the list 1

Types of List Controls • List. Box tool – 1 st prefix – Simple List Box with/without scroll bars • Combo. Box tool – cbo prefix – List may allow for user to add new items – List may "drop down" to display items in list 2

List Controls Visually Dropdown Combo Box Simple Combo Box List Boxes Dropdown List Combo Box List Boxes Combo Boxes 3

Choosing List Type • Many items, little space – Dropdown Combo Box – Dropdown List Combo Box • Few items – List Box • User allowed to add to the list – Simple Combo Box – Dropdown Combo Box 4

Name and Text Properties • List. Boxes – Name displayed at Design Time • Combo. Boxes – Text displayed and accessible at Design Time – Text also accessible at Run Time – If user enters new data in Combo. Box then VB stores it temporarily in the Text property 5

Drop. Down. Style Property • Used to indicate the type of Combo. Box – Dropdown Combo – Simple Combo – Dropdown List User cannot enter text at run time Drop. Down Simple Drop. Down. List User can enter text at run time 6

Items Collection • List of items in a List. Box or Combo. Box is a collection • Collections are objects that have properties and methods that allow you to – Add items – Remove items – Refer to individual items – Count items – Clear the collection 7

Index Property • Zero based value used to reference individual items in the collection • Position of an item in the list – 1 st item – 2 nd item – 3 rd item Index = 0 Index = 1 Index = 2 (1 -1=0) (2 -1=1) (3 -1=2) • So we can say that. . . – nth item Index = n-1 8

Selected. Index Property • Use to identify currently selected item in the list • If no item is selected Selected. Index = -1 • Use to select an item in the list 9

Items. Count Property • Use to determine number of items in the list Remember: Items. Count is the actual number of items in the list BUT the Selected. Index property and Index will be 1 less. For example, it there are 20 items in the list: Items. Count=20 BUT Index =19 for the last item in list 10

Using the Items Collection • Use the index of the item to reference a specific item in the collection • Remember that the index is zero based so the 1 st item in the list is index position zero lst. Schools. Items(5) = "USC" ' Next line references the currently selected item str. Selected. Flavor = lst. Flavor. Items(lst. Flavor. Selected. Index) 11

Filling a List • At Design time in properties window 1. Go to Items property 2. Click on ellipses to open String Collection Editor 3. Separate items with ENTER key • At Run time methods – Items. Add OR – Items. Insert 12

Filling List - Design Time Click ellipses button to open 13

Items. Add Method • Use to add new items to the list at run time • General Form • Examples Object. Items. Add(Item. Value) lst. Names. Items. Add("Brandon") lst. Dept. Nums. Items. Add(100) cbo. Majors. Items. Add(txt. Majors. Text) 'Next line adds new item user typed in to combo cbo. Grade. Items. Add(cbo. Grade. Text) 14

Items. Insert Method • Use to add new items to the list at run time at a specific location(index) in the collection • General Form Object. Items. Insert(Index. Position, Item. Value) • Examples lst. Dept. Nums. Items. Insert(1, 100) cbo. Grade. Items. insert(0, "Freshman") cbo. Majors. Items. Insert(11, txt. Majors. Text) 15

Items. Remove. At Method • Use to remove specific items from the list at run time by specifying the index of the item • General Form Object. Items. Remove. At(Index. Position) • Examples lst. Dept. Nums. Items. Remove. At(1) ' Next line removes the currently selected item cbo. Grade. Items. Remove. At(cbo. Grade. Selected. Index) 16

Items. Remove Method • Use to remove specific items from the list at run time by specifying the Text of the item • General Form Object. Items. Remove(Text. String) • Examples lst. Dept. Nums. Items. Remove("USC") ' Next line removes the currently selected item cbo. Grade. Items. Remove(cbo. Grade. Text) 17

Clear Method • Empty the entire list from list box or combo box, remove all the items • General Form • Examples Object. Items. Clear( ) lst. Dept. Nums. Items. Clear( ) cbo. Grade. Items. Clear( ) 18
- Slides: 18