Upload
syedkareemhkg
View
1.111
Download
14
Embed Size (px)
Citation preview
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
1Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Chapter 5:The Repetition Process in
Visual Basic
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
2Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
The Repetition Process
• The capability to repeat one or more statements as many times as necessary is what really sets a computer apart from other devices
• All loops have two parts: – the body of the loop (the statements being
repeated)– a termination condition that stops the loop
• Failure to have a valid termination condition can lead to an endless loop
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
3Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Types of Loops• There are three types of loops
– event-driven– determinate – indeterminate
• Event-driven loops are repeated by the user causing an event to occur
• Determinate loops repeat a known number of times
• Indeterminate loops repeat an unknown number of times
• Variables should be initialized before being used in a loop
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
4Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Event-Driven Loops
• Event-driven loops are repeated by user causing an event to occur
• Variable scope is important for loops; Variables in event procedures are local and are reset to zero when procedure terminates
• Variables defined at form level are known to all procedures and retain their value between events
• Form-level variables are declared in the Declarations procedure of the General object
• Static variables retain their value between events but are local to event procedure
• Declared with Static keyword
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
5Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Form-level Variables
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
6Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Use of AddItem and Clear Methods
• The AddItem method is used to add items to a list box at run time
• Form of AddItem method
list1.Additem string
• Always add a string to list box since it contains text
• The Clear method clears a list boxlist1.Clear
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
7Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Code to Sum a Series of Numbers
Private Sub cmdCalc_Click() Dim intTheValue As Integer intTheValue = CInt(txtTheValue.Text) intSum = intSum + intTheValue ‘intSum is form level intNumValues = intNumValues + 1 ‘form level txtSum.Text = Str(intSum) txtNumValues.Text = Str(intNumValues) lstEntries.AddItem str(intTheValue) txtTheValue.Text = "” txtTheValue.SetFocusEnd Sub
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
8Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Determinate Loops Using For-Next Loop
Best way to create a determinate loop is to use a For-Next Loop
Form of For-Next Loop:For variable = start value to end value Step change value statements that compose body of loopNext variable
where variable = the counter variable in the loopstart value = the beginning value of the counter variableend value = the ending value of the counter variable
change value = the amount the counter variable changes each time through the loop
Next variable = the end of the For loop
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
9Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Example of For-Next Loop
Counter Variable
Beginning Value
Ending Change Value Value
intSum = intSum + intCounter
Body of Loop
For intCounter = 1 to 10 Step 1For Statement
Next intCounter
Next Statement
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
10Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Code To Sum using For-Next Loop
Private Sub cmdCalc_Click() Dim intTheValue as Integer, intSum as Integer Dim intNumValues as Integer, intCounter as Integer intSum = 0 ‘Initialize Sum to Zero If txtNumValues.Text = "" Then ’Number not entered Msgbox "Please enter number of values to be summed“ Exit Sub ’Do not go into loop End ifintNumValues = CInt(txtNumValues.Text)For intCounter = 1 to intNumValues
intTheValue = CInt(InputBox("Enter next value")) intSum = intSum + intTheValue lstEntries.AddItem Str(intTheValue) ‘Add to listNexttxtSum.Text = Str(intSum)lstEntries.AddItem "Sum is " & Str(intSum)
End Sub
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
11Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Printing a List
• To print the contents of listbox, use the Listcount and List() properties of the list box
• Listcount is the number of items in list box but List() property runs from 0 to Listcount -1
• List(intCounter) is equal to the contents of the corresponding list box
• Code to print contents of list box to Immediate Window:
For intCounter = 0 to lstEntries.ListCount - 1 Debug.Print lstEntries.List(intCounter)
Next
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
12Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Indeterminate Loops• Indeterminate loops run for an unknown number of
repetitions until a condition is true or while a condition is true
• Four types of indeterminate loops– Until loop with termination condition before body of loop – While loop with termination condition before body of loop– Until loop with termination condition after body of loop– While loop with termination condition after body of loop
• Pre-Test loops have termination condition before loop body
• Post-test loops have termination condition after loop body
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
13Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Form of Pre- and Post-Test Loops
• The form of the pre-test loops is:Do Until (or While) conditionbody of loop
Loop
• The form of the post-test loops is:Dobody of loop
Loop Until (or While) condition
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
14Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Pre and Post-Test Loops
Do While condition statement1 statement2 (continued)Loopnext statement
True Condition (loop continues) False Condition (loop ends)
Do While condition statement1 statement2 (continued)Loopnext statement
Do While Loop
Do Until Loop
Do Until condition statement1 statement2 (continued)Loopnext statement
True Condition (loop ends) False Condition (loop continues)Do Until condition statement1 statement2 (continued)Loopnext statement
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
15Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Processing an Unknown Number of Values from a File
• A data file is a collection of data stored on magnetic or optical secondary storage in the form of records.
• A record is a collection of one or more data items that are treated as a unit.
• Files are identified by the computer’s operating system with file names assigned by the user.
• Files are important to processing data into information because they provide a permanent method of storing large amounts of data that can be input whenever needed.
• Three types of files: sequential access, database, and direct access files
• We will use sequential access files as input.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
16Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Using Sequential Access Files
• Sequential access files must be read in same order as they are created so they replicate the action of entering data from a keyboard.
• The number of records on a sequential access file is often unknown, but there is an invisible binary marker at the end of the file called the EOF (end of file) marker.
• Use a Do While loop or a Do Until loop to input data from sequential access file.
• Loops can input data until the EOF marker is encountered (or while it has not been encountered).
• Create sequential access files by using a text editor such as Notepad.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
17Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Opening and Inputting Data from a File
• Files must be opened with the Open statement:Open “filename" for Input as #n
• To input data from a file, use the Input #n, statement
Input #n, list of variables
• Files must be closed at end of procedure Close #n
• Using an Until loop to input data from fileDo Until EOF(n)
Input #n, list of variables
Loop
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
18Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Code to Input and Sum values from File
Private Sub cmdCalc_Click() Dim intTheValue As Integer, intSum As Integer Dim intNumValues As Integer Open "a:\SumData.txt" For Input As #10 intSum = 0 intNumValues = 0 Do Until EOF(10) ’Input to end of file Input #10, intTheValue intSum = intSum + intTheValue intNumValues = intNumValues + 1 lstEntries.AddItem Str(intTheValue) Loop txtNumValues.Text = Str(intNumValues) txtSum.Text = Str(intSum) lstEntries.AddItem "Sum is " & str(intSum) Close #10End Sub
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
19Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
The Combo Box
• A combo box is a combination of a text box and a list box.
• It has a text box portion that is displayed at all times and a drop-down list of items that can be displayed by clicking on the down arrow.
• One property of note for the combo box is the Style property, which can be set at design time to Drop Down combo (the default), Simple Combo, or Drop Down list. Its prefix is cbo .
• The combo box has all the properties and methods of the list box including the AddItem, ListCount, and List() properties.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
20Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
More on Combo Boxes
• The default event for the Combo box is the Change event--to use the Click event, you must change to it.
• The Sorted property is a useful property for the combo and list boxes that arranges the items in the box alphabetically.
• Items can be removed from a combo or list box with the RemoveItem method which requires that number of the item to be remove be given. To remove a selected item, you can use the ListIndex property, eg,
cboMembers.RemoveItem cboMembers.ListIndex
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
21Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Nested Loops• A Nested loop is a loop within a loop. Must complete the
inner loop within the outer loop.• Nested For-Next loops have a For-Next loop within a For-
Next loop. The inner loop will go through all its values for each value of the outer loop.
• Three key programming rules to remember about using nested For-Next loops:
• Always use different counter variables for the outer and inner For-Next loops.
• Always have the Next statement for the inner For-Next loop before the Next statement for the outer For-Next loop.
• Always include the counter variable in the Next statements to distinguish between the loops.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
22Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Creating an Executable File
• An executable file is one that can be executed on any computer on which it is installed.
• Creating an executable file in VB is accomplished with the File|Make filename.exe menu selection.
• Once an executable file is created, a shortcut to it can be created by right-clicking the file name in Windows Explorer and selecting Create Shortcut.
• Drag the shortcut to the desktop.
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
23Copyright © 2001 by Wiley. All rights reserved.
Chapter 5:The Repetition Process in Visual Basic
Event Driven Loops
Determinate Loops
Indeterminate Loops
Sequential Access Files
Combo Boxes
Executable Files
Debugging Loops
• Debug a loop by inserting a debug.print command in the loop to print to the Immediate Window.
• Add a Quick Watch by locating the pointer on a variable and clicking the eyeglass icon on the Debug Toolbar. The values for this variable will be shown in the Watch Window.
• Use the Locals window to display the values of variables local to a procedure.
• Use the Toggle Breakpoint icon to pause execution at a designated line in the code and then use the various windows to view the values for variables.