Chapter 33. Printing

Most of the programs described in earlier lessons display output on the computer's screen. Lessons 31 and 32 explain how to save output in files.

This lesson explains a third method for saving output: printing. Using these techniques, you can print text, shapes, images — just about anything you want.

Warning

Before you start a printing project, however, be warned that printing in Visual Basic isn't trivial. It's easy enough to display some text or a few lines in a printout, but producing a complex formatted document can be a lot of work.

If you need to produce a nicely formatted resume, graph, or grid of values, you should ask yourself whether there's an easier way than writing a bunch of Visual Basic code. For example, Microsoft Word is great at producing nicely formatted text documents, and Microsoft Excel does a wonderful job of making charts and graphs. You can certainly generate these sorts of printouts using Visual Basic, but it may be a lot faster if you use another tool such as Word or Excel.

BASIC PRINTING

The PrintDocument component sits at the center of the printing process. To print, a program creates an instance of this class either at design time or at run time. It adds event handlers to catch the object's events and then lets the object do its thing. As the object generates pieces of the printout, it raises events to let the program supply graphics for it to print.

The PrintDocument object raises four key events:

  • BeginPrint — Raised when the object is ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.