vb011 list boxes vb15. vb012 list boxes a list box contains a list of items one underneath the other...

34
VB01 1 List Boxes VB15

Upload: leon-knight

Post on 05-Jan-2016

228 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 1

List Boxes

VB15

Page 2: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 2

List Boxes

• A list box contains a list of items one underneath the other

DanPeterJaneRobertElaineNiamhAlan

Page 3: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 3

lstName.AddItem

lstNaughty.AddItem “Peter”

Adds Peter to the list called lstNaughty

Peter

Page 4: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 4

lstName.AddItem

lstNaughty.AddItem “Mike”

Adds Mike to the list called lstNaughty

PeterMike

Page 5: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 5

lstName.Clear

lstNaughty.Clear

Removes all the items from the list box

Page 6: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 6

lstName.List (int)

RodJaneFreedyZippyBungleJeferyArcher

Individual list box items can be referenced by a subscript just like an array

lstNaughty.List (0) is “Rod”

lstNaughty.List (3) is “Zippy”

lstNaughty.List (4) is “Bungle”

Page 7: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 7

lstName.List (int)

RodJaneFreedyZippyBungleJeferyArcher

The .list method can also be used to change an item.

lstNaughty.List (3) = “Peter”

RodJaneFreedyPeterBungleJeferyArcher

Page 8: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 8

lstName.ListCount

• The .ListCount method is used to determine the number of items in the list

• lntNaughty.ListCount is 7

RodJaneFreedyPeterBungleJeferyArcher

Page 9: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 9

lstName.RemoveItem (int)

• The .RemoveItem method is used to remove a particular item

• lntNaughty.RemoveItem (4)

RodJaneFreedyPeterBungleJeferyArcher

RodJaneFreedyPeterJeferyArcher

Page 10: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 10

Using List Boxes- Example 1

RodJaneFreedyZippy

PatRory

Add name

Naughty

Nice

Naughty NiceSanta’s List

Page 11: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 11

Santa’s List

RodJaneFreedyZippy

PatRory

Add name

Naughty

Nice

Naughty Nice

lstNaughty lstNice

optNaughty

txtName

Page 12: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 12

Santa’s List - Code

strName = txtName.Text

If optNaughty.value then

lstNaughty.AddItem strName

Else

lstNice.AddItem strName

End If

Page 13: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 13

Using List Boxes- Example 2

• lstNaughty is a list of names• Write a VB program that

counts the number names in the list that begin with the letter R and puts this number into a variable called intTotalCount

RodJaneRyuichiRoryBungleJeferyArcher

Page 14: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 14

Algorithm

Set intTotalCount = 0

For each name

If it begins with “R” then

Add one to intTotalCount

Next name

Page 15: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 15

Dim intTotalCount As IntegerDim strName as StringintTotalCount = 0For n = 0 to lstNaughty.ListCount - 1

strName = lstNaughty.List (n)If left (strName, 1) = “R” Then

intTotalCount = intTotalCount + 1End If

Next n

Page 16: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 16

Dim intTotalCount As IntegerDim strName as StringintTotalCount = 0For n = 0 to lstNaughty.ListCount - 1

strName = lstNaughty.List (n)If left (strName, 1) = “R” Then

intTotalCount = intTotalCount + 1End If

Next n

Page 17: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 17

Dim intTotalCount As IntegerDim strName as StringintTotalCount = 0For n = 0 to lstNaughty.ListCount - 1

strName = lstNaughty.List (n)If left (strName, 1) = “R” Then

intTotalCount = intTotalCount + 1End If

Next n

Page 18: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 18

Dim intTotalCount As IntegerDim strName as StringintTotalCount = 0For n = 0 to lstNaughty.ListCount - 1

strName = lstNaughty.List (n)If left (strName, 1) = “R” OR left (strName, 1) = “r” Then

intTotalCount = intTotalCount + 1End If

Next n

Page 19: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 19

Using List Boxes - Example 3

RodJaneRyuichiRoryBungleJeferyArcher

9874596

Two list boxes lstNames and lstAges are used to store the names and ages of children respectively.

The nth item in lstAges is the age of the child whose name in the nth item in lstNames

Write a program that finds the name of the youngest child and stores it in strYoungest

Page 20: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 20

Algorithm• Set strYoungest to the first name in lstNames• Set the intYoungestSoFar to the first age in

lstAges• Go though the ages one by one• For each item in intAges• If it is smaller than intYoungestSoFar then

– Set intYoungerstSoFar to it– Set strYoungest to the corresponding name

• Go on to the next one

Page 21: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 21

Dim strYoungest As StringDim intYoungestSoFar As IntegerstrYoungest = lstNames.List(0)intYoungestSoFar = lstAges.List(0)For n = 0 to lstAges.ListCount - 1

If lstAges.List(n) < intYoungestSoFar ThenintYoungestSoFar = lstAges.List(n)strYoungest = lstNames.List(n)

End IfNext n

Page 22: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 22

Dim strYoungest As StringDim intYoungestSoFar As IntegerstrYoungest = lstNames.List(0)intYoungestSoFar = lstAges.List(0)For n = 0 to lstAges.ListCount - 1

If lstAges.List(n) < intYoungestSoFar ThenintYoungestSoFar = lstAges.List(n)strYoungest = lstNames.List(n)

End IfNext n

Page 23: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 23

Dim strYoungest As StringDim intYoungestSoFar As IntegerstrYoungest = lstNames.List(0)intYoungestSoFar = lstAges.List(0)For n = 0 to lstAges.ListCount - 1

If lstAges.List(n) < intYoungestSoFar ThenintYoungestSoFar = lstAges.List(n)strYoungest = lstNames.List(n)

End IfNext n

Page 24: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 24

Dim strYoungest As StringDim intYoungestSoFar As IntegerstrYoungest = lstNames.List(0)intYoungestSoFar = lstAges.List(0)For n = 0 to lstAges.ListCount - 1

If lstAges.List(n) < intYoungestSoFar ThenintYoungestSoFar = lstAges.List(n)strYoungest = lstNames.List(n)

End IfNext n

Page 25: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 25

Dim strYoungest As StringDim intYoungestSoFar As IntegerstrYoungest = lstNames.List(0)intYoungestSoFar = lstAges.List(0)For n = 1 to lstAges.ListCount - 1

If lstAges.List(n) < intYoungestSoFar ThenintYoungestSoFar = lstAges.List(n)strYoungest = lstNames.List(n)

End IfNext n

Page 26: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 26

Using List Boxes - Example 3

RodJaneRyuichiRoryBungleJeferyArcher

910

71415

96

Two list boxes lstNames and lstAges are used to store the names and ages of children respectively.

The nth iten in lstAges is the age of the child whose name in the nth item in lstNames

Write a program that removes all of the children who are 10 years of age or older

Page 27: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 27

RodJaneRyuichiRoryBungleJeferyArcher

910

71415

96

RodRyuichiJeferyArcher

9796

Page 28: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 28

Algorithm

For each child

If age >= 10 then

Delete that childs name from the list

End If

Next child

Page 29: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 29

For n = 0 to lstAges.ListCount - 1

If lstAges.List (n) >= 10 Then

lstNames.RemoveItem (n)

End If

Next n

Page 30: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 30

RodJaneRyuichiRoryBungleJeferyArcher

910

71415

96

RodRyuichiRoryJefery

910

71415

96

What happened!!???

Page 31: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 31

RodJaneRyuichiRoryBungleJeferyArcher

910

71415

96

RodRyuichiRoryJefery

910

71415

96

What happened!!???

The ages weren’t removed

Wrong names removed

Page 32: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 32

For n = 0 to lstAges.ListCount - 1

If lstAges.List (n) >= 10 Then

lstNames.RemoveItem (n)

lstAges.RemoveItem (n)

End If

Next n This will remove ages

Page 33: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 33

For n = 0 to lstAges.ListCount - 1

If lstAges.List (n) >= 10 Then

lstNames.RemoveItem (n)

lstAges.RemoveItem (n)

As items are deleted the size of the list box changes.

Does n count to whatever ListCount-1 was at the start or does it check ListCount every time? ?

Page 34: VB011 List Boxes VB15. VB012 List Boxes A list box contains a list of items one underneath the other Dan Peter Jane Robert Elaine Niamh Alan

VB01 34

n = 0

Do Until n > lstAge.ListCount

If lstAges.List (n) >= 10 then

lstAges.RemoveItem (n)

lstNames.RemoveItem (n)

Else

n = n + 1

End If

Loop