Chapter 7 Loops and Printing Programming In Visual Basic .NET Do/Loops • Repeating a series of instructions • An iteration is a single execution of the statement(s) in the loop • Used when the exact number of iterations is unknown 7- 2 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Do/Loops (continued) • Terminates based on a specified condition – Loop While a condition is True – Loop Until a condition becomes True • Condition can be placed at – Top of loop - Pretest – Bottom of loop - Posttest 7- 3 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. The Do and Loop Statements General Form Do {While |Until} condition ' Statements in loop. Loop OR Do ' Statements in loop. Loop {While | Until} condition 7- 4 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 running • Do While … Loop • Do Until … Loop • Posttest, loop will always be executed at least once • Do … Loop While • Do … Loop Until 7- 5 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Do While vs. Do Until • Do While a condition is true or false Loop userEntry = False Do while errorFlag = False … If len(customerName.textbox) > 0 Then … userEntry = True Else … End If Loop 7- 6 Condition False True Condition False True © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Do While vs. Do Until: Pretest • Do While a condition is true or false Loop userEntry = False Do until errorFlag = True … If len(customerName.textbox) > 0 Then … userEntry = True Else … End If Loop 7- 7 Condition True False Condition False True © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Do While vs. Do Until: Posttest • Do While a condition is true or false Loop userEntry = True Do … False Condition If len(customerName.textbox) > 0 Then … True userEntry = True userEntry Else = True … End If Loop Until userEntry = True Condition False (or Loop While userEntry = False) True 7- 8 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. For/Next Loops • Use when you know the number of iterations • Uses a numeric counter variable, called Loop Index, to control number of iterations • Loop 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 number 7- 9 © 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. Next [LoopIndex] 7- 10 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. For/Next Loop • For example: Dim customerCount as Integer For customerCount = 1 to 10 ... If customerType = “Regular” Then … Else End If Next 7- 11 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Exiting For/Next Loops • In some situations you may need to exit the loop prematurely • Use the Exit For statement inside the loop structure • Generally the Exit For statement is part of an If statement 7- 12 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. The PrintDocument Component • Add a PrintDocument component to form – Appears in the Component Tray • Execute the Print method to start printing • Logic for actual printing belongs in the PrintDocument's PrintPage event procedure 7- 13 © 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 callback • BeginPrint and EndPrint are also fired at the beginning and end of the printing • PrintPage 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 page 7- 14 © 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 printer • Can contain strings of text and graphic elements • Specify the exact X and Y coordinates of each element to be printed on the page 7- 15 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Using the DrawString Method • Used to send a line of text to the graphics page • Belongs to the Graphics object of the PrintPageEventArgs argument • Is an overloaded method so there are several forms for calling the method • Set up the Font to be used before executing the DrawString method 7- 16 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. The DrawString Method (cont) General Form DrawString(StringToPrint, Font, Brush, Xcoordinate, Ycoordinate) Examples 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) 7- 17 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Setting the X and Y Coordinates • For each print line, specify X and Y coordinates • Create variables declared as Single to set the X and Y values 7- 18 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. PrintPageEventArgs • PrintPageEventArgs argument has several useful properties • MarginBounds – Code as • e.MarginBounds.Left • e.MarginBounds.Right • e.MarginBounds.Top • e.MarginBounds.Bottom • PageBounds • PageSettings 7- 19 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Aligning Decimal Columns • • • • 7- 20 It is important to align the decimal points of numeric data Proportional fonts make aligning decimal points difficult Declare an object as a SizeF Structure Use 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( ) ' Set X for left-aligned column. horizontalPrintLocationSingle = 200 ' Set ending position for right-aligned column. columnEndSingle = 500 ' Format the number. formattedOutputString= amountDecimal.ToString("C") ' Calculate the X position of the amount. ' Measure string in this font. fontSizeF= e.Graphics.MeasureString(formattedOutputString, _ printFont) 7- 21 © 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 7- 22 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Displaying a Print Preview • Add PrintPreviewDialog component to form – Appears in the Component Tray – Default name is fine • Assign in code the same PrintDocument object you are using for printing • Execute the ShowDialog method of the PrintPreviewDialog component 7- 23 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Using Static Variables • Static local variables retain their value for the life of the project • Can be useful for – Running totals – Running counts – Boolean switches – Storing current page number/count when printing multiple pages 7- 24 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Printing Multiple Pages • Recall 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 True 7- 25 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.