Chapter 17
Working with Printers
After completing this chapter, you will be able to:
Print graphics from a Microsoft Visual Basic program.
Print text from a Visual Basic program.
Print multipage documents.
Create Print, Page Setup, and Print Preview dialog boxes in your programs.
In the following sections, you'll complete your survey of user interface design and components by learning how to add printer support to your Windows applications. Microsoft Visual Basic 2005 supports printing by offering the PrintDocument class and its many objects, methods, and properties, which facilitate printing.
Upgrade Notes: Migrating Visual Basic 6 Code to Visual Basic 2005
If you're experienced with Microsoft Visual Basic 6, you'll notice some new features in Visual Basic 2005, including the following:
In Visual Basic 6, printing is accomplished by using the methods and properties of the Printer object. For example, the Printer.Print method sends a string of text to the default printer. In Microsoft Visual Basic .NET 2003 and Visual Basic 2005, printing is accomplished by using the PrintDocument class, which provides more functionality than the older method but is also more complex.
In Visual Basic 6, you have access to one predefined dialog box for printing services—the Print dialog box provided by the CommonDialog ActiveX control. In Visual Basic 2005, you have access to several predefined dialog box controls for printing, including PrintDialog, PrintPreviewDialog, and PageSetupDialog.
To implement multipage printing in Visual Basic 2005, you must create a PrintPage event handler that prints each page of your document one at a time. Although managing this printing process can be somewhat involved, it's simplified by services in the System.Drawing.Printing namespace.
In this chapter, you'll learn how to print graphics and text from Visual Basic programs, manage multipage printing tasks, and add printing dialog boxes to your user interface. In my opinion, this chapter is one of the most useful in the book, with lots of practical code that you can immediately incorporate into real-world programming projects. Printing support doesn't come automatically in Visual Basic 2005, but the routines in this chapter will help you print longer text documents and display helpful dialog boxes such as Page Setup, Print, and Print Preview from within your programs. I'll start the chapter with two very simple printing routines to show you the basics, and then I'll get considerably more sophisticated.