Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 7 Loops and Printing Programming In Visual Basic.NET."— Presentation transcript:

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

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

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

4 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 4 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)

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

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

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

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

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

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

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

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

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

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

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

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

17 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 17 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)

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

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

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

21 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 21 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)

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

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

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

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


Download ppt "Chapter 7 Loops and Printing Programming In Visual Basic.NET."

Similar presentations


Ads by Google