420 Part III Designing the User Interface
If you look closely, you see the following dialog box appear when Visual Basic sends
your print job to the printer:
This status box is also a product of the PrintDocument class, and it provides users
with a professional-looking print interface, including the page number for each
printed page.
- Type additional paths if you like, and then click the Print Graphics button for more
printouts. - When you’re finished experimenting with the program, click the Close button on the
form.
The program stops. Not bad for your first attempt at printing from a Visual Basic program!
Printing Text from a Text Box Object
You’ve had a quick introduction to the PrintDocument control and printing graphics. Now
try using a similar technique to print the contents of a text box on a Visual Basic form. In the
following exercise, you’ll build a simple project that prints text by using the PrintDocument
class, but this time you’ll define the class by using program code without adding the
PrintDocument control to your form. In addition, you’ll use the Graphics.DrawString method
to send the entire contents of a text box object to the default printer.
Note The following program is designed to print one page or less of text. To print multiple
pages, you need to add additional program code, which will be explored later in the chapter.
My goal is to introduce one new printing feature at a time.
Use the Graphics .DrawString method to print text
- Click the Close Project command on the File menu, and then create a new Windows
Forms Application project named My Print Text.
A blank form opens. - Use the Label control to draw a label object near the top of the form.
This label will display a line of instructions for the user.