Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1
415

Chapter 17


Working with Printers


After completing this chapter, you will be able to:
n Print graphics from a Visual Basic program.
n Print text from a Visual Basic program.
n Print multipage documents.
n 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 2010 supports printing with the PrintDocument class. The PrintDocument class
and its many methods, properties, and supporting classes handle sending text and graphics
to printers.

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 2010, 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.

Using the PrintDocument Class


Most Windows applications allow users to print documents after they create them, and by
now you might be wondering just how printing works in Visual Basic programs. This is one
area where Visual Basic 2010 has lots of power and flexibility, but this impressive technical
sophistication comes at a little cost. Producing printed output from Visual Basic programs
isn’t a trivial process, and the technique you use depends on the type and amount of
printed output you want to generate. In all cases, however, the fundamental mechanism
that regulates printing in Visual Basic 2010 is the PrintDocument class, which you can create
in a project in two ways:

n By adding the PrintDocument control to a form
n By defining it programmatically with a few lines of Visual Basic code

Table of Contents


Working with Printers..................................... 415


Using the PrintDocument Class...................................... 415
Printing Text from a Text Box Object............................. 420
Printing Multipage Text Files......................................... 424
One Step Further: Adding Print Preview and Page Setup Dialog Boxes..... 430
Chapter 17 Quick Reference.......................................... 437
Free download pdf