15
Information Technology, Engineering Faculty UNIM ListBox & ComboBox Academic Year: 2014-2015 UNIM (3 rd Week) Forms & More Controls (Part 3)

3rd Week - Forms and More Controls (Part 3)

Embed Size (px)

DESCRIPTION

VB.Net Programming, ListBox and ComboBox, Information Technology

Citation preview

Page 1: 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)

Page 2: 3rd Week - Forms and 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

Page 3: 3rd Week - Forms and More Controls (Part 3)

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)

Page 4: 3rd Week - Forms and More Controls (Part 3)

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]

Page 5: 3rd Week - Forms and More Controls (Part 3)

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

Page 6: 3rd Week - Forms and More Controls (Part 3)

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);

Page 7: 3rd Week - Forms and More Controls (Part 3)

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”);

Page 8: 3rd Week - Forms and More Controls (Part 3)

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

Page 9: 3rd Week - Forms and More Controls (Part 3)

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;

Page 10: 3rd Week - Forms and More Controls (Part 3)

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;

Page 11: 3rd Week - Forms and More Controls (Part 3)

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)

Page 12: 3rd Week - Forms and More Controls (Part 3)

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()

Page 13: 3rd Week - Forms and More Controls (Part 3)

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()

Page 14: 3rd Week - Forms and More Controls (Part 3)

Exercise

Page 15: 3rd Week - Forms and More Controls (Part 3)

Finish…!