8
D 975 A P P E N D I X D ADDING PRINTING CAPABILITIES TO AN APPLICATION » In this appendix, you learn how to use the PrintDocument control to add printing capabilities to an application. C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 975

ADDING PRINTING CAPABILITIES TO AN APPLICATION

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ADDING PRINTING CAPABILITIES TO AN APPLICATION

D

975

A P P E N D I X D

ADDING PRINTINGCAPABILITIES TO ANAPPLICATION

» In this appendix, you learn how to use the PrintDocument

control to add printing capabilities to an application.

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 975

Page 2: ADDING PRINTING CAPABILITIES TO AN APPLICATION

976

A D D I N G P R I N T I N G C A P A B I L I T I E S T O A N A P P L I C A T I O N

USING THE PRINTDOCUMENTCONTROL

In Chapter 10, you viewed the code for the Friends application; you also viewed a sample run of the application. As you may remember, the application displays the names ofyour friends in a combo box. It also allows you to add names to and remove names from thecombo box. When the application ends, the form’s FormClosing event procedure saves thecontents of the combo box in a sequential access file named friends.txt. This appendix usesa modified version of the Friends application. In the modified version, the interface con-tains a Print button that allows you to print the contents of the combo box. Figure D-1shows the interface used in the application.

Figure D-1: Interface for the modified Friends application

You can use a PrintDocument control in an application to send output to the printer.You instantiate (create) a PrintDocument control using the PrintDocument tool in thetoolbox.

To add a PrintDocument control to the Friends application:

1 Start Visual Studio 2005 or Visual Basic 2005 Express Edition, if necessary, and closethe Start Page window.

2 Open the Friends Solution (Friends Solution.sln) file, which is contained in theVB2005\AppD\Friends Solution folder. If necessary, open the designer window andpermanently display the Toolbox window.

3 Click the PrintDocument tool in the toolbox. The tool is located in the Printing sec-tion of the toolbox, as shown in Figure D-2. Drag the tool to the form, then release themouse button. A PrintDocument control appears in the component tray, as shown inFigure D-2.

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 976

Page 3: ADDING PRINTING CAPABILITIES TO AN APPLICATION

977

A P P E N D I X D

Now that you have added the PrintDocument control to the interface, you can code thePrint button’s Click event procedure, which will print the contents of thexFriendComboBox. You print information using the PrintDocument control’s Printmethod. The method’s syntax is printdocument.Print(), where printdocument is thename of a PrintDocument control.

To code the Print button’s Click event procedure:

1 Auto-hide the Toolbox window. Open the Code Editor window, then open the codetemplate for the xPrintButton’s Click event procedure.

2 Type ‘ prints the contents of the xFriendComboBox and press Enter twice. Typeme.printdocument1.print() and press Enter.

3 Save the solution.

The PrintDocument control’s Print method causes the PrintDocument control’sPrintPage event to occur. In the PrintPage event procedure, you specify the informationyou want to print. You also indicate the way you want the information to appear in the

PrintDocumentcontrol

PrintDocumenttool

Figure D-2: PrintDocument control shown in the component tray

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 977

Page 4: ADDING PRINTING CAPABILITIES TO AN APPLICATION

978

A D D I N G P R I N T I N G C A P A B I L I T I E S T O A N A P P L I C A T I O N

printout. In this case, the printout should look similar to the sample printout shown inFigure D-3. Notice that the printout contains a header followed by a blank line and thena list of your friends’ names.

Figure D-3: Sample printout

header

blank line

list of friends’ names

My Friends

First friend’s name

.

.

Last friend’s name

To open the PrintDocument control’s PrintPage event procedure:

1 Open the code template for the PrintDocument1 control’s PrintPage event procedure.

2 Type ‘ specifies the content and appearance of the printout and press Enter twice.See Figure D-4.

e parameter

The e parameter’s Graphics.DrawString method allows you to print text on the printer.

THE E.GRAPHICS.DRAWSTRING METHODYou use the e.Graphics.DrawString method to print text on the printer. The method’ssyntax is e.Graphics.DrawString(string, font, brushes, horizontalPosition, verticalPosition).In the syntax, string is the text to print, and font is a Font variable that indicates the name,size, and style of the print font. The brushes argument indicates the color of the text andtypically is set to Brushes.Black. The horizontalPosition and verticalPosition argumentsdetermine the location of the text from the left edge and top edge, respectively, of theprinted page.

Figure D-4: PrintDocument1 control’s PrintPage event procedure

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 978

Page 5: ADDING PRINTING CAPABILITIES TO AN APPLICATION

979

A P P E N D I X D

Some print fonts are proportionally spaced, while others are fixed-spaced, oftenreferred to as mono-spaced. A fixed-spaced font uses the same amount of space to printeach character, whereas a proportionally spaced font uses varying amounts of space toprint characters. For example, using a fixed-spaced font, the wide letter W and thenarrow letter l occupy the same amount of print space. Using a proportionally spacedfont, on the other hand, the letter W occupies more print space than the letter l does.Courier New is an example of a fixed-spaced font; Microsoft Sans Serif is an example ofa proportionally spaced font. In most cases, you use a fixed-spaced font when printinginformation, because a fixed-spaced font allows you to align the text that appears in theprintout.

To code the PrintDocument control’s PrintPage event procedure:

1 First you will declare a Font variable that indicates the name, size, and style of theprinter font. Type ‘ declare the printer font and press Enter. Type dim printFont asnew font(“courier new”, 12, fontstyle.regular) and press Enter.

2 Now you will declare a variable that will keep track of the vertical position of the texton the printed page. Type ‘ declare a variable for the vertical position and pressEnter, then type dim vertical as integer and press Enter twice.

3 Next, you will print the header along with two newline characters. The header text will beprinted a distance of 10 points from the left edge of the page, and a distance of 10 pointsfrom the top of the page. Type ‘ print header and press Enter. Type e.graphics.drawstring(“My Friends” _ and press Enter. Press Tab, then type & controlchars.newline & controlchars.newline, _ and press Enter. Type printfont, brushes.black,10, 10) and press Enter twice.

4 You will print the first friend’s name a distance of 80 points from the top of the page.Type vertical = 80 and press Enter.

5 Now you will use a For Each...Next loop to print each combo box entry on a separateline. Type for each name as string in xFriendComboBox.items and press Enter.Type e.graphics.drawstring(name, printfont, brushes.black, 10, vertical) andpress Enter. You will print the next name a distance of 15 points from the previousname. Type vertical = vertical + 15.

6 Change the Next clause to Next name. Figure D-5 shows the code entered in thePrintDocument1 control’s PrintPage event procedure. It also shows the code enteredin the xPrintButton’s Click event procedure.

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 979

Page 6: ADDING PRINTING CAPABILITIES TO AN APPLICATION

980

A D D I N G P R I N T I N G C A P A B I L I T I E S T O A N A P P L I C A T I O N

To print the contents of the combo box:

1 Close the Code Editor window, then save the solution.

2 Start the application. Two names appear in the xFriendComboBox, as shown inFigure D-6.

Visual Basic code

Private Sub xPrintButton_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles xPrintButton.Click

' prints the contents of the xFriendComboBox

Me.PrintDocument1.Print()

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs) _

Handles PrintDocument1.PrintPage

' specifies the content and appearance of the printout

' declare the printer font

Dim printFont As New Font("courier new", 12, FontStyle.Regular)

' declare a variable for the vertical position

Dim vertical As Integer

' print header

e.Graphics.DrawString("My Friends" _

& ControlChars.NewLine & ControlChars.NewLine, _

printFont, Brushes.Black, 10, 10)

vertical = 80

For Each name As String In xFriendComboBox.Items

e.Graphics.DrawString(name, printFont, Brushes.Black, 10,

vertical)

vertical = vertical + 15

Next name

End Sub

Figure D-5: Code you entered in this appendix

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 980

Page 7: ADDING PRINTING CAPABILITIES TO AN APPLICATION

981

A P P E N D I X D

3 If your computer is connected to a printer, click the Print button.

4 Click the Exit button to end the application, then close the solution.

Figure D-6: Friends application

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 981

Page 8: ADDING PRINTING CAPABILITIES TO AN APPLICATION

C6220_Appendix D_Rev1.qxd 10/15/06 11:04 AM Page 982