Slide Repetition Control Structures 2021
Slide Repetition Control Structures 2021
Control Structures II
Ghana Technology University College
Lecturer – Dr. Forgor Lempogo
2021
The previous Lesson
We discussed:
Control structures
Sequential
Selection
Radio buttons
Message boxes
2
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Today’s Lesson
Control structures
Repetition
Pretest loop
The condition is evaluated before the instructions within
the loop are processed
The instructions may be processed zero or more times
Posttest loop
The condition is evaluated after the instructions within the
loop are processed
The instructions are always processed at least once
Do...Loop
For...Next Loop
5
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Do...Loop Statement
Do...Loop statement:
codes both a pretest or posttest loop
Do While <condition>
statement(s)
Loop
As a Posttest
Do
statement(s)
Loop While <condition>
8
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Do while Structure - Example
As a Pretest
Dim number As Integer = 1
Do While number <= 7
MessageBox.Show(number.ToString)
number = number +1
Loop
As a Posttest
Dim number As Integer = 1
Do
MessageBox.Show(number.ToString)
number = number +1
Loop While number <= 7
9
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Do While- example
The following program requires the user to enter a
number from 1 through 3.
txtQuotation.Text = quotation
11
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Do – Until Structure
As a Pretest
Do Until <condition>
statement(s)
Loop
As a Posttest
Do
statement(s)
Loop Until <condition>
12
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Do Until Structure - Example
As a Pretest
Dim number As Integer = 1
Do Until number > 7
MessageBox.Show(number.ToString)
number = number +1
Loop
As a Posttest
Dim number As Integer = 1
Do
MessageBox.Show(number.ToString)
number = number +1
Loop Until number > 7
13
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Do – Until - example
Suppose you deposit money into a savings account and
let it accumulate at 6 percent interest compounded
annually.
The following program determines when you will be a
millionaire:
Control Property Setting
Form Name frmMillionaire
Text 6% Interest
Button1 Name btnCalculate
Text Analyse
Textbox1 Name txtAmount
Textbox 2 ReadOnly True
Name txtWhen
Label 1 Name lblAmount
Text Amount to Deposit (GH Cedis)
14
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Do - Until - example (2)
15
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The For...Next Statement
For...Next statement:
processes a set of instructions a known number of times
Is a pretest loop
Counter-controlled loop:
a loop whose processing is controlled by a counter
Counter variable:
used to keep track of the number of times the loop has been
processed
Eg.
For i = m To n Step s
21
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The For...Next Statement (3)
Can use For…Next or Do…Loop for a counter-
controlled loop
22
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The For...Next Statement (4)
23
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Select the Best Loop
Use a Do loop if the number of repetitions is unknown and
is based on a condition changing; a For...Next loop is best
if the exact number of repetitions is fixed
InputBox function:
Displays a predefined dialog box that allows the user to
enter data
Contains a text message, an OK button, a Cancel button,
and an input area
27
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The InputBox Function (continued)
Structure of the Input function:
28
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The InputBox Function (continued)
InputBox function parameters:
defaultResponse:
a prefilled value for the user input
Example
firstName = InputBox("Enter your First Name:", "SalesTech", "John")
29
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Sales Express Application
30
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Sales Express Application (GUI)
31
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Sales Express Application (Code)
' calculates and displays the average sales amount
Const Prompt As String = "Enter a sales amount. Click Cancel to end."
Const Title As String = "Sales Entry"
Dim inputSales As String = String.Empty
Dim salesCounter As Integer
Dim salesAccumulator, salesAverage, sales As Decimal
' get first sales amount
inputSales = lnputBox(Prompt, Title, "0")
Do While inputSales <> String.Empty
Decimal.TryParse(inputSales, sales)
salesCounter = salesCounter + 1
salesAccumulator = salesAccumulator + sales
' get the next sales amount
inputSales = lnputBox(Prompt, Title, "0")
Loop
If salesCounter > 0 Then
salesAverage = salesAccumulator I Convert.ToDecimal(salesCounter)
averagelabel.Text = salesAverage.ToString("C2")
Else
32 Averagelabel.Text = "$0.00“
End If CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using a List Box in an Interface
List box:
displays a list of choices from which the user can
select zero or more choices
SelectionMode property:
controls the number of choices a user can select
None: user can scroll but not select anything
One: user can select one item
MultiSimple and MultiExtended: user can select
multiple items
33 CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Adding Items to a List Box
Items collection:
a collection of the items in a list box
Collection:
a group of one or more individual objects treated as one unit
Add method:
adds an item to the list box’s Items collection
Items to be added must be converted to String
36
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Accessing Items in a List Box
Index:
37
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Accessing Items in a List Box
Syntax
object.Items(index)
Example 1
Dim animalType As String= String.Empty
animalType = Convert. ToString(animallistBox. ltems(0))
Assigns the first item in the animalListBox to a string variable.
You can also use the statement:
animalType = animalListBox.ltems(0).ToString
Dim code As integer
Code = Convert.Tolnt(codelistBox.ltems(2))
Assigns the third item in the codelistbox to an integer variable;
You can use the statement:
38lnteger.TryParse(codelistBox.ltems(2).ToString, code)
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Determining the Number of Items
in a List Box
Items.Count property:
39
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Determining Number of Items in ListBox
Syntax
object.Items.Count
Example 1
Dim numberOfAnimals As Integer
numberOfAnimals = animallistBox.ltems.Count
assigns the number of items contained in the animalListBox to
an Integer variable
Dim numCodes, index As Integer
numCodes = codelistBox.ltems.Count
Do While index < numCodes
MessageBox.Show(Convert.ToString(codelistBox.ltems(index)))
index = index + 1
Loop
SelectedIndex property:
Contains the index of the selected item in the list
If nothing is selected, it contains the value -1
42
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Select a Default ListBox Item
Example 1 (SelectedItem Property)
animalListBox.SelectedItem = “Cat”
Selects the cat item in the animalListBox
48
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Product Finder Application
51
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Form_Load Event
Private Sub MainForm_Load(ByVal sender As Object, _
ByVal e As Syst1em.EventArgs) Handles Me.Load
‘fills lhe list box with IDs
idListBox.ltems.Add("FX123")
id ListBox.ltems.Add("AB654”)
idListBox.ltems.Add("JH733”)
idListBox.ltems.Add("FX457")
idlListBox.ltems.Add("NK111”)
idListBox.ltems.Add("KYT897”)
idListBox.ltems.Add("KVB419”)
idListBox.Items.Add(" PQR333")
idListBox.ltems.Add("UVP492”)
End Sub
52
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Find Button Click Event
Dim isFound As Boolean, id As String = String.Empty
Dim index, numberOfltems As Integer
id = idTextBox.Text.ToUpper
numberOfltems = idlistBox.ltems.Count
Do While index < numberOfltems AndAlso isFound = False
If id= idlistBox.ltems(index).ToString.ToUpper Then
isFound = True
Else
index = index + 1
End If
Loop
If isFound = True Then
idlistBox.Selectedlndex = index
Else
idlistBox.Selectedlndex = -1
MessageBox.Show("Not found", "Product Finder",_
MessageBoxButtons.OK, MessageBoxlcon.lnformation)
53
End If
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The Combo Box Control
Combo Box control:
Similar to a list box with a list of choices
List portion may be hidden
May contain a text field that allows the user to type an
entry that is not on the list
55
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Insert Data into Combo Box
' fills the combo boxes with values
nameComboBox.Items.Add("Amy")
nameComboBox.Items.Add("Beth")
nameComboBox.Items.Add("Carl")
nameComboBox.Items.Add("Dan")
nameComboBox.Items.Add("Jan")
nameComboBox.Selectedindex = 0
cityComboBox.Items.Add("London")
cityComboBox.Items.Add("Madrid")
cityComboBox.Items.Add("Paris")
cityComboBox.SelectedItem = "Madrid"
stateComboBox.Items.Add("Alabama")
stateComboBox.Items.Add("Maine")
stateComboBox.Items.Add("New York" )
stateComboBox.Items.Add("South Dakota")
stateComboBox.Text
56 = "New York"
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Properties of the Combo Box
Items.Add method:
used to add items to a combo box
SelectedItem property:
contains the value of the selected item in the list
Text property:
contains the value that appears in the text portion of the control
(item selected or typed in)
Items.count property:
used to obtain the number of items in the combo box
Sorted property:
used to sort the items in the list
57
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using an Image List Control
Image List control:
Stores a collection of images
Does not appear on the form; appears in
the component tray
58
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using an Image List Control (2)
59
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Using an Image List Control (3)
60
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Select a Default ListBox Item
Syntax
object.lmages.ltem(index)
Example 1
Imagelist1.Images.Item(0)
PictureBox1.Image = ImageList1.Images.Item(index)
64
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Previous Button Click
numImages = ImageList1.Images.Count
line9:
If index < numImages And index >= 0 Then
PictureBox1.Image = ImageList1.Images.Item(index)
index -= 1
Else
index = numImages - 1
GoTo line9
End If
65
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Next Button Click
numImages = ImageList1.Images.Count
line9:
If index < numImages And index >= 0 Then
PictureBox1.Image = ImageList1.Images.Item(index)
index += 1
Else
index = 0
GoTo line9
End If
66
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Slide Show Button Click
numImages = ImageList1.Images.Count
For index As Integer = 0 To numImages - 1
PictureBox1.Image = ImageList1.Images.Item(index)
Me.Refresh()
System.Threading.Thread.Sleep(5000)
Next index
67
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Any Questions?
68
CICS 313: Visual Basic Programming - GTUC 2021 Delivery