Chapter 7 Loops and Printing Programming In Visual Basic.NET.

Slides:



Advertisements
Similar presentations
Chapter 7 Lists, Loops, and Printing Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

Programming with Microsoft Visual Basic th Edition
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Repeating Actions While and For Loops
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Chapter 7 Lists, Loops, and Printing
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
Chapter 5: Loops and Files.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CHAPTER THREE Representing Data: Constants and Variables.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 12: How Long Can This Go On?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CSC 162 Visual Basic I Programming. Repetition Structures Pretest Loop –Exit condition is tested before the body of code is executed Posttest Loop –Exit.
Chapter 7 Loops Programming In Visual Basic.NET Prepared by Johnny Tsui,
Chapter 6: The Repetition Structure
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Visual Basic Programming
Chapter 7 Lists, Loops, and Printing Programming In Visual Basic.NET.
‘Tirgul’ # 2 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #2.
Introduction to Problem Solving and Control Statements.
Chapter 7 P 1 Lists and Loops List boxes and combo boxes List box - list of items - useful for a list which does not change Combo box - list of items -
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 7 - Lists, loops and printing w List boxes and combo boxes several types can add items at design time or during run time user select from predefined.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Lists, Loops, and Printing Chapter 7.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
List Boxes and Combo Boxes Provides a list of items to select from Various styles — choose based on Space available Need to select from an existing list.
Controlling Program Flow with Looping Structures
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
CHAPTER THREE Representing Data: Constants and Variables.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Problem Solving and Control Statements: Part 2
3rd prep. – 2nd Term MOE Book Questions.
Chapter (3) - Looping Questions.
Iteration: Beyond the Basic PERFORM
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Repetition and Multiple Forms
Chapter 7 Lists, Loops, and Printing
Presentation transcript:

Chapter 7 Loops and Printing Programming In Visual Basic.NET

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Do/Loops Repeating a series of instructionsRepeating a series of instructions An iteration is a single execution of the statement(s) in the loopAn iteration is a single execution of the statement(s) in the loop Used when the exact number of iterations is unknownUsed when the exact number of iterations is unknown

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Do/Loops (continued) Terminates based on a specified conditionTerminates based on a specified condition –Loop While a condition is True –Loop Until a condition becomes True Condition can be placed atCondition can be placed at –Top of loop - Pretest –Bottom of loop - Posttest

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved The Do and Loop Statements - General Form Do {While |Until} condition ' Statements in loop. ' Statements in loop.LoopORDo Loop {While | Until} condition Top of Loop Condition, Pretest (condition checked before the loop exectures Bottom of Loop Condition, Posttest (condition checked after the loop executes)

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Pretest vs. Posttest Pretest, loop may never be executed since tested BEFORE runningPretest, loop may never be executed since tested BEFORE running Do While … LoopDo While … Loop Do Until … LoopDo Until … Loop Posttest, loop will always be executed at least oncePosttest, loop will always be executed at least once Do … Loop WhileDo … Loop While Do … Loop UntilDo … Loop Until

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Do While vs. Do Until Do While a condition is true or false userEntry = False Do while errorFlag = False … If len(customerName.textbox) > 0 ThenDo While a condition is true or false userEntry = False Do while errorFlag = False … If len(customerName.textbox) > 0 Then … userEntry = True Else … End If Loop Condition True False True False Loop

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Do While vs. Do Until: Pretest Do While a condition is true or false userEntry = False Do until errorFlag = True … If len(customerName.textbox) > 0 Then … userEntry = True Else … End If LoopDo While a condition is true or false userEntry = False Do until errorFlag = True … If len(customerName.textbox) > 0 Then … userEntry = True Else … End If Loop Condition False True False Loop

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Do While vs. Do Until: Posttest Do While a condition is true or false userEntry = True Do … If len(customerName.textbox) > 0 Then … userEntry = True Else … End If Loop Until userEntry = TrueDo While a condition is true or false userEntry = True Do … If len(customerName.textbox) > 0 Then … userEntry = True Else … End If Loop Until userEntry = True (or Loop While userEntry = False) Condition True False True False Loop userEntry = True Condition

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved For/Next Loops Use when you know the number of iterationsUse when you know the number of iterations Uses a numeric counter variable, called Loop Index, to control number of iterationsUses a numeric counter variable, called Loop Index, to control number of iterations Loop Index is incremented at the bottom of the loop on each iterationLoop Index is incremented at the bottom of the loop on each iteration Step value can be included to specify the incrementing amount to increment Loop Index, step can be a negative numberStep value can be included to specify the incrementing amount to increment Loop Index, step can be a negative number

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved The For and Next Statements - General Form For LoopIndex = InitialValue To TestValue [Step Increment] ' Statements in loop. ' Statements in loop. Next [LoopIndex]

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved For/Next Loop For example: Dim customerCount as Integer For customerCount = 1 to If customerType = “Regular” Then … Else End If NextFor example: Dim customerCount as Integer For customerCount = 1 to If customerType = “Regular” Then … Else End If Next

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Exiting For/Next Loops In some situations you may need to exit the loop prematurelyIn some situations you may need to exit the loop prematurely Use the Exit For statement inside the loop structureUse the Exit For statement inside the loop structure Generally the Exit For statement is part of an If statementGenerally the Exit For statement is part of an If statement

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved The PrintDocument Component Add a PrintDocument component to formAdd a PrintDocument component to form –Appears in the Component Tray Execute the Print method to start printingExecute the Print method to start printing Logic for actual printing belongs in the PrintDocument's PrintPage event procedureLogic for actual printing belongs in the PrintDocument's PrintPage event procedure

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Setting Up the Print Output PrintPage event is fired once for each page to be printed, referred to as callbackPrintPage event is fired once for each page to be printed, referred to as callback BeginPrint and EndPrint are also fired at the beginning and end of the printingBeginPrint and EndPrint are also fired at the beginning and end of the printing PrintPage event includes the argument e as System.Drawing.Printing.PrintPageEventArgsPrintPage event includes the argument e as System.Drawing.Printing.PrintPageEventArgs Properties of the PrintPageEventArgs are useful for handling page margins and sending strings of text to the pageProperties of the PrintPageEventArgs are useful for handling page margins and sending strings of text to the page

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved The Graphics Page Set up graphics page in memory and then page is sent to the printerSet up graphics page in memory and then page is sent to the printer Can contain strings of text and graphic elementsCan contain strings of text and graphic elements Specify the exact X and Y coordinates of each element to be printed on the pageSpecify the exact X and Y coordinates of each element to be printed on the page

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Using the DrawString Method Used to send a line of text to the graphics pageUsed to send a line of text to the graphics page Belongs to the Graphics object of the PrintPageEventArgs argumentBelongs to the Graphics object of the PrintPageEventArgs argument Is an overloaded method so there are several forms for calling the methodIs an overloaded method so there are several forms for calling the method Set up the Font to be used before executing the DrawString methodSet up the Font to be used before executing the DrawString method

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved The DrawString Method (cont) General Form General Form Examples DrawString(StringToPrint, Font, Brush, Xcoordinate, Ycoordinate) e.Graphics.DrawString(printLineString, printFont, Brushes.Black, _ horizontalPrintLocationSingle, verticalPrintLocationSingle) e.Graphics.DrawString("My text string", myFont, Brushes.Black, _ 100.0, 100.0) e.Graphics.DrawString(nameTextBox.Text, New Font("Arial", 10), _ Brushes.Red, leftMarginSingle, currentLineSingle)

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Setting the X and Y Coordinates For each print line, specify X and Y coordinatesFor each print line, specify X and Y coordinates Create variables declared as Single to set the X and Y valuesCreate variables declared as Single to set the X and Y values

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved PrintPageEventArgs PrintPageEventArgs argument has several useful propertiesPrintPageEventArgs argument has several useful properties MarginBoundsMarginBounds –Code as e.MarginBounds.Lefte.MarginBounds.Left e.MarginBounds.Righte.MarginBounds.Right e.MarginBounds.Tope.MarginBounds.Top e.MarginBounds.Bottome.MarginBounds.Bottom PageBoundsPageBounds PageSettingsPageSettings

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Aligning Decimal Columns It is important to align the decimal points of numeric dataIt is important to align the decimal points of numeric data Proportional fonts make aligning decimal points difficultProportional fonts make aligning decimal points difficult Declare an object as a SizeF StructureDeclare an object as a SizeF Structure Use MeasureString method of the Graphics class to determine the width of a formatted string in pixelsUse MeasureString method of the Graphics class to determine the width of a formatted string in pixels

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Aligning Decimal Columns Code Example ' SizeF structure for font size info. Dim fontSizeF As New SizeF( ) Dim fontSizeF As New SizeF( ) ' Set X for left-aligned column. ' Set X for left-aligned column. horizontalPrintLocationSingle = 200 horizontalPrintLocationSingle = 200 ' Set ending position for right-aligned column. ' Set ending position for right-aligned column. columnEndSingle = 500 columnEndSingle = 500 ' Format the number. ' Format the number. formattedOutputString= amountDecimal.ToString("C") formattedOutputString= amountDecimal.ToString("C") ' Calculate the X position of the amount. ' Calculate the X position of the amount. ' Measure string in this font. ' Measure string in this font. fontSizeF= e.Graphics.MeasureString(formattedOutputString, _ printFont) fontSizeF= e.Graphics.MeasureString(formattedOutputString, _ printFont)

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Aligning Decimal Columns Code Example (cont) ' SizeF structure for font size info (cont). ' Subtract width of string from the column position. columnXSingle = columnEndSingle - fontSizeF.Width ' Set up the line--each element separately. e.Graphics.DrawString("The Amount = ", printFont, _ Brushes.Black, horizontalPrintLocationSingle, _ verticalPrintLocationSingle) e.Graphics.DrawString(formattedOutputString, printFont, _ Brushes.Black, columnXSingle, verticalPrintLocationSingle) ' Increment line for next line. verticalPrintLocationSingle += lineHeightSingle

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Displaying a Print Preview Add PrintPreviewDialog component to formAdd PrintPreviewDialog component to form –Appears in the Component Tray –Default name is fine Assign in code the same PrintDocument object you are using for printingAssign in code the same PrintDocument object you are using for printing Execute the ShowDialog method of the PrintPreviewDialog componentExecute the ShowDialog method of the PrintPreviewDialog component

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Using Static Variables Static local variables retain their value for the life of the projectStatic local variables retain their value for the life of the project Can be useful forCan be useful for –Running totals –Running counts –Boolean switches –Storing current page number/count when printing multiple pages

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Printing Multiple Pages Recall that the PrintDocument's PrintPage event fires once for each pageRecall that the PrintDocument's PrintPage event fires once for each page Indicate that there are more pages to print by setting the HasMorePages property of the PrintPageEventArgs to TrueIndicate that there are more pages to print by setting the HasMorePages property of the PrintPageEventArgs to True