This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy
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
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
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
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
• 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
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
• 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
• 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
• 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
• 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.