Chapter 7 Loops and Printing Programming In Visual

  • Slides: 25
Download presentation
Chapter 7 Loops and Printing Programming In Visual Basic. NET © 2004 by The

Chapter 7 Loops and Printing Programming In Visual Basic. NET © 2004 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Do/Loops • Repeating a series of instructions • An iteration is a single execution

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 2 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Do/Loops (continued) • Terminates based on a specified condition – Loop While a condition

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 3 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Do and Loop Statements General Form Do {While |Until} condition ' Statements in

The Do and Loop Statements General Form Do {While |Until} condition ' Statements in loop. Loop OR Do ' Statements in loop. Loop {While | Until} condition 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 Mc. Graw-Hill Companies, Inc. All rights reserved.

Pretest vs. Posttest • Pretest, loop may never be executed since tested BEFORE running

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 5 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Do While vs. Do Until • Do While a condition is true or false

Do While vs. Do Until • Do While a condition is true or false Loop user. Entry = False Do while error. Flag = False … If len(customer. Name. textbox) > 0 Then … user. Entry = True Else … End If Loop 6 Condition False True © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Do While vs. Do Until: Pretest • Do While a condition is true or

Do While vs. Do Until: Pretest • Do While a condition is true or false Loop user. Entry = False Do until error. Flag = True … If len(customer. Name. textbox) > 0 Then … user. Entry = True Else … End If Loop 7 Condition True False Condition False True © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Do While vs. Do Until: Posttest • Do While a condition is true or

Do While vs. Do Until: Posttest • Do While a condition is true or false Loop user. Entry = True Do … False Condition If len(customer. Name. textbox) > 0 Then … True user. Entry = True user. Entry Else = True … End If Loop Until user. Entry = True Condition False (or Loop While user. Entry = False) True 8 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

For/Next Loops • Use when you know the number of iterations • Uses a

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 9 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The For and Next Statements General Form For Loop. Index = Initial. Value To

The For and Next Statements General Form For Loop. Index = Initial. Value To Test. Value [Step Increment] ' Statements in loop. Next [Loop. Index] 10 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

For/Next Loop • For example: Dim customer. Count as Integer For customer. Count =

For/Next Loop • For example: Dim customer. Count as Integer For customer. Count = 1 to 10. . . If customer. Type = “Regular” Then … Else End If Next 11 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Exiting For/Next Loops • In some situations you may need to exit the loop

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 12 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Print. Document Component • Add a Print. Document component to form – Appears

The Print. Document Component • Add a Print. Document component to form – Appears in the Component Tray • Execute the Print method to start printing • Logic for actual printing belongs in the Print. Document's Print. Page event procedure 13 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Setting Up the Print Output • Print. Page event is fired once for each

Setting Up the Print Output • Print. Page event is fired once for each page to be printed, referred to as callback • Begin. Print and End. Print are also fired at the beginning and end of the printing • Print. Page event includes the argument e as System. Drawing. Print. Page. Event. Args • Properties of the Print. Page. Event. Args are useful for handling page margins and sending strings of text to the page 14 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Graphics Page • Set up graphics page in memory and then page is

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 15 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Using the Draw. String Method • Used to send a line of text to

Using the Draw. String Method • Used to send a line of text to the graphics page • Belongs to the Graphics object of the Print. Page. Event. Args argument • Is an overloaded method so there are several forms for calling the method • Set up the Font to be used before executing the Draw. String method 16 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Draw. String Method (cont) General Form Draw. String(String. To. Print, Font, Brush, Xcoordinate,

The Draw. String Method (cont) General Form Draw. String(String. To. Print, Font, Brush, Xcoordinate, Ycoordinate) Examples e. Graphics. Draw. String(print. Line. String, print. Font, Brushes. Black, _ horizontal. Print. Location. Single, vertical. Print. Location. Single) e. Graphics. Draw. String("My text string", my. Font, Brushes. Black, _ 100. 0, 100. 0) e. Graphics. Draw. String(name. Text. Box. Text, New Font("Arial", 10), _ Brushes. Red, left. Margin. Single, current. Line. Single) 17 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Setting the X and Y Coordinates • For each print line, specify X and

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 18 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Print. Page. Event. Args • Print. Page. Event. Args argument has several useful properties

Print. Page. Event. Args • Print. Page. Event. Args argument has several useful properties • Margin. Bounds – Code as • e. Margin. Bounds. Left • e. Margin. Bounds. Right • e. Margin. Bounds. Top • e. Margin. Bounds. Bottom • Page. Bounds • Page. Settings 19 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Aligning Decimal Columns • • 20 It is important to align the decimal points

Aligning Decimal Columns • • 20 It is important to align the decimal points of numeric data Proportional fonts make aligning decimal points difficult Declare an object as a Size. F Structure Use Measure. String method of the Graphics class to determine the width of a formatted string in pixels © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Aligning Decimal Columns Code Example ' Size. F structure for font size info. Dim

Aligning Decimal Columns Code Example ' Size. F structure for font size info. Dim font. Size. F As New Size. F( ) ' Set X for left-aligned column. horizontal. Print. Location. Single = 200 ' Set ending position for right-aligned column. End. Single = 500 ' Format the number. formatted. Output. String= amount. Decimal. To. String("C") ' Calculate the X position of the amount. ' Measure string in this font. Size. F= e. Graphics. Measure. String(formatted. Output. String, _ print. Font) 21 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Aligning Decimal Columns Code Example (cont) ' Size. F structure for font size info

Aligning Decimal Columns Code Example (cont) ' Size. F structure for font size info (cont). ' Subtract width of string from the column position. column. XSingle = column. End. Single - font. Size. F. Width ' Set up the line--each element separately. e. Graphics. Draw. String("The Amount = ", print. Font, _ Brushes. Black, horizontal. Print. Location. Single, _ vertical. Print. Location. Single) e. Graphics. Draw. String(formatted. Output. String, print. Font, _ Brushes. Black, column. XSingle, vertical. Print. Location. Single) ' Increment line for next line. vertical. Print. Location. Single += line. Height. Single 22 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Displaying a Print Preview • Add Print. Preview. Dialog component to form – Appears

Displaying a Print Preview • Add Print. Preview. Dialog component to form – Appears in the Component Tray – Default name is fine • Assign in code the same Print. Document object you are using for printing • Execute the Show. Dialog method of the Print. Preview. Dialog component 23 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Using Static Variables • Static local variables retain their value for the life of

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 24 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Printing Multiple Pages • Recall that the Print. Document's Print. Page event fires once

Printing Multiple Pages • Recall that the Print. Document's Print. Page event fires once for each page • Indicate that there are more pages to print by setting the Has. More. Pages property of the Print. Page. Event. Args to True 25 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.