230 likes | 314 Views
Using Looping Structures and Lists. Chapter 4. For … Next Statements Repeat a specific number of times. For intCounter = 1 To 5 Statement block Next intCounter intCounter values are 0,[1, 2, 3, 4, 5], 6. intCounter = 0. intCounter = 6.
E N D
Using Looping Structures and Lists Chapter 4
For … Next Statements Repeat a specific number of times • For intCounter = 1 To 5 • Statement block • Next intCounter • intCounter values are 0,[1, 2, 3, 4, 5], 6 intCounter = 0 intCounter = 6
For … Next Statements Repeat a specific number of times • For intCounter = 5 To 1 Step -1 • Statement block • Next intCounter • intCounter values are 0,[5, 4, 3, 2, 1], 0
For … Next Statements Repeat a specific number of times • Counter must be a numeric variable, usually integer. • Startvalue, Endvalue, and Stepvalue are numeric, usually integer, can be constants or variables. • Startvalue, Endvalue can be positive, negative or zero. • Stepvalue can be positive or negative but not zero. • Stepvalue defaults to 1 is omitted.
For … Next Example • Write a For Next loop where the user inputs the names of 4 players on a bowling team and displays them on different lines in a multi-line text box called txtPlayers.
For … Next Example • For intCounter = 1 To 4 • strName = strName & InputBox(“Type a name for player “ & intCounter) & vbCrLf • Next intCounter • txtPlayers.Text = strName
For … Next Example • intNbrOnTeam = Val(InputBox(“How many people are on the team?”)) • For intCounter = 1 To intNbrOnTeam • strName = strName & InputBox(“Type a name for player “ & intCounter) & vbCrLf • Next intCounter • txtPlayers.Text = strName
Debug Technique • intNbrOnTeam = Val(InputBox(“How many people are on the team?”)) • For intCounter = 1 To intNbrOnTeam • strName = strName & InputBox(“Type a name for player “ & intCounter) & vbCrLf • Debug.Writeline(intCounter) • Debug.Writeline(strName) • Next intCounter • txtPlayers.Text = strName
Create a List of Interest Rates • sngBegRate = Val(InputBox(“What is the beginning interest rate?”)) • sngEndRate = Val(InputBox(“What is the ending interest rate?”)) • For sngRate = sngBegRate To sngEndRate Step .05 • strRates = strRates & format(sngRate,”Percent”) & vbCrLf • Next sngCounter • txtRates.Text = strRates
Endless Loops • A loop which will never reach the end value is an endless, or infinite, loop. • This is the worst error you can make in coding. • Changing the Counter value inside the loop can produce an endless loop. • To exit an endless loop, press the ctrl and break keys together.
Incrementing Variables intCounter = IntCounter + 1 intCounter += 1 Each takes the value of integer counter, adds one to it, and stores the result in integer counter.
Working with Lists Use a • ComboBox when the user needs to type in their own selection, this creates a possibility of input errors. • ListBox – when the user is limited to the specific choices listed, no possibility of input errors.
ListBox Properties • Items – String Collection Editor • Index – points to a particular item in the list, indexes begin with 0. • SelectionMode • None – no items may be selected. • One – only one item may be selected at a time. • MultiSimple – more than one item may be selected. • MultiExtended – more than one item may be selected.
Items.Add method • Adds items to the collection during run time. • Used for ComboBoxes and ListBoxes. • Listboxname.Items.Add value • Lists are usually populated in the Form load procedure.
Adding items to a list at run time. • Listboxname.Items.Add value • Numeric values can be added in a loop • For sngRate = .05 To .12 Step .01 • lstRates.Items.Add sngRate • Next sngRate • lstRates.SetSelected = (2, True)
ListBox.SetSelected method • listBox.SetSelected (index, value) • The index refers to which item is selected. • The selected value is either true or false. • lstRates.SetSelected = (2, True) • Selects the third item in the list as the default.
Determine the selected item • A listbox used the SelectedIndex property to determine which item is selected. • This only works for single select list boxes.
Which rate is selected? • Select Case lstRates.SelectedIndex • Case 0 • Case 1 • Case 2 • Case 3 • End Select
Display Payments in a List Box • lstRates.Clear ‘first clear the list • For intCtr = 1 To intTerm * 12 • lstPayments.Items.Add (“Payment #” & intCtr & vbTab & Format(Pmt(sngRate/12, intTerm * 12, -decPrincipal), “Currency”) • Next
For…Each…Next Statement • This statement allows us to work with each item in a collection. • A collection is a group of individual objects treated as one unit. • Items in a ComboBox or ListBox are one kind of collection. • Other collections are in Chapter 8.
For…Each…Next Statement • Executes the statements in the loop for each element in a collection. • For Each element In collection • Statements • Next [element] • Element is an object variable which keeps track of each item in a collection.
For…Each…Next Statement • For Each objControl In Controls • If TypeOf objControl IS TextBox Then • objTextBox = objControl • If objTextBox.Visible Then • objTextBox.Visible = False • Else • objTextBox.Visible = True • End If • End If • Next objControl
List Folders on a Disk Drive • Dim strLetter As String • Dim folders() As DirectoryInfo • lstDir.Items.Clear() • strLetter = InputBox(“Enter a drive letter.”) • Folders = New DirectoryInfo(strLetter & “:\”) • Dim folder as DirectoryInfo • For Each folder In folders • lstDir.Items.Add(folder.FullName) • Next