vb011 list boxes vb15. vb012 list boxes a list box contains a list of items one underneath the other...
TRANSCRIPT
VB01 1
List Boxes
VB15
VB01 2
List Boxes
• A list box contains a list of items one underneath the other
DanPeterJaneRobertElaineNiamhAlan
VB01 3
lstName.AddItem
lstNaughty.AddItem “Peter”
Adds Peter to the list called lstNaughty
Peter
VB01 4
lstName.AddItem
lstNaughty.AddItem “Mike”
Adds Mike to the list called lstNaughty
PeterMike
VB01 5
lstName.Clear
lstNaughty.Clear
Removes all the items from the list box
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”
VB01 7
lstName.List (int)
RodJaneFreedyZippyBungleJeferyArcher
The .list method can also be used to change an item.
lstNaughty.List (3) = “Peter”
RodJaneFreedyPeterBungleJeferyArcher
VB01 8
lstName.ListCount
• The .ListCount method is used to determine the number of items in the list
• lntNaughty.ListCount is 7
RodJaneFreedyPeterBungleJeferyArcher
VB01 9
lstName.RemoveItem (int)
• The .RemoveItem method is used to remove a particular item
• lntNaughty.RemoveItem (4)
RodJaneFreedyPeterBungleJeferyArcher
RodJaneFreedyPeterJeferyArcher
VB01 10
Using List Boxes- Example 1
RodJaneFreedyZippy
PatRory
Add name
Naughty
Nice
Naughty NiceSanta’s List
VB01 11
Santa’s List
RodJaneFreedyZippy
PatRory
Add name
Naughty
Nice
Naughty Nice
lstNaughty lstNice
optNaughty
txtName
VB01 12
Santa’s List - Code
strName = txtName.Text
If optNaughty.value then
lstNaughty.AddItem strName
Else
lstNice.AddItem strName
End If
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
VB01 14
Algorithm
Set intTotalCount = 0
For each name
If it begins with “R” then
Add one to intTotalCount
Next name
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
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
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
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
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
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
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
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
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
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
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
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
VB01 27
RodJaneRyuichiRoryBungleJeferyArcher
910
71415
96
RodRyuichiJeferyArcher
9796
VB01 28
Algorithm
For each child
If age >= 10 then
Delete that childs name from the list
End If
Next child
VB01 29
For n = 0 to lstAges.ListCount - 1
If lstAges.List (n) >= 10 Then
lstNames.RemoveItem (n)
End If
Next n
VB01 30
RodJaneRyuichiRoryBungleJeferyArcher
910
71415
96
RodRyuichiRoryJefery
910
71415
96
What happened!!???
VB01 31
RodJaneRyuichiRoryBungleJeferyArcher
910
71415
96
RodRyuichiRoryJefery
910
71415
96
What happened!!???
The ages weren’t removed
Wrong names removed
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
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? ?
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