Download - 3rd Week - Forms and More Controls (Part 3)
Information Technology, Engineering Faculty
UNIM
ListBox & ComboBox
Academic Year: 2014-2015
UNIM
(3rd Week)
Forms & More Controls (Part 3)
List Boxes & Combo Boxes
Types of list boxes and combo boxes
1. Simple list boxes
2. Simple combo boxes
3. Drop-down combo boxes
4. Drop-down lists
List boxes and combo boxes have many common properties a
nd operate similarly
Combo box control has a DropDownStyle property to determin
e if list box has a text box for user entry and whether list will dro
p down
Scroll bars are added automatically if the box is too small to
display all items in list
Combo box displays Text property in control
List Boxes & Combo Boxes … (2)
The Items Collection
List of items in a list box or combo box is a collection
Collections are objects with properties and methods
Items collection used to add items, remove items, ref
er to items, count the items, and clear the list
Refer to items in collection by an index which starts a
t zero
Refer to first item in the Items collection as Items[0]
Filling a List
• Click on ellipses button next to Items property to open String C
ollection Editor
• Use String Collection Editor to add items to list
• To add items during execution, use the Items.Add or Items.Inse
rt method
Using the Items.Add Method
• General Form
Object.Items.Add(ItemValue);
New item added to end of the list
Set the control’s Sorted property to true to place item alphabetical
ly in list
Must use the Add method to add value user types in text box of co
mbo box to the list
Example:
coffeeComboBox.Items.Add(coffeeComboBox.Text);
Using the Items.Insert Method
Choose location for a new item in the list by specifying the ind
ex position
General Form
Object.Items.Insert(IndexPosition, ItemValue);• Index is zero based
• Do not set the list control’s Sorted property to true if using the Insert method
• Example:
schoolsListBox.Items.Insert(0,”Harvard”);
The SelectedIndex Property
• SelectedIndex property contains index number of item the us
er selects (highlights)
• If no list item is selected, SelectedIndex is set to negative 1 (-1)
• Example:
coffeeTypesListBox.SelectedIndex = 3; //Select fourth item in list
The Items.Count Property
• Count property of Items collection contains number of items in
the list
• Items.Count is always one more than the highest possible Sele
ctedIndex
• Example:
intTotalItems = itemsListBox.Items.Count;
Referencing the Items Collection
Specify an element by including an index
General Form Object.Items(IndexPosition)
Highest index is Items.Count – 1
Combine Items property and SelectedIndex property to refer to
currently selected element
Example:
strSelectedFlavor = FlavorListBox.Items(FlavorListBox.SelectedIndex)
Retrieve selected list item with Text property of control
Example:
selectedMajorLabel.Text = majorsComboBox.Text;
Removing an Item from a List
Specify the index or text of item to remove it from a list
Use Items.RemoveAt method to remove by index
General Form
Object.Items.RemoveAt(IndexPosition)
Use Items.Remove method to remove by text
General Form
Object.Items.Remove(TextString)
7- 12
Clearing a List
Use Items.Clear method to empty a combo box or list box
General Form
Object.Items.Clear()
Example:
schoolsListBox.Items.Clear()
7- 13
Clearing a List
Use Items.Clear method to empty a combo box or list box
General Form
Object.Items.Clear()
Example:
schoolsListBox.Items.Clear()
Exercise
Finish…!