Browse DevX
Sign up for e-mail newsletters from DevX


Expand Your VB6 Printing Repertoire—Part I

If you've ever tried to write code to print a complex document using VB, you'll appreciate how the reusable tools described in this solution beef up VB's printing capabilities and add Print Preview support.

Figure 1. Printing and Previewing Plain Text: Here's the main form of the TextPrinting Project in Part II of this series, along with the print preview form from the same project.

art I of this solution series explores VB's basic printing concepts: the Printer object, page geometry, graphics methods, and the Print common dialog control. In this solution, you'll see how to control where printing occurs on a page.

Building on the knowledge gained from Part I, in Part II you'll build a text editor and text-printing utility that includes font selection and print-preview capabilities. The VB6 TextBox control provides the base functionality you need to build the text editor; but what good is a text editor without printing capabilities? Figure 1 shows the main form of the TextPrinting project and the print-preview form from the same project. When sent to a printer, the text will print just as it appears on the preview form, within the specified margins.

Author's Note: Printing formatted text is a very different story. The RichTextBox control provides a Print method, which you can use to send the control's text to the printer. Printing formatted text is a different task from printing unformatted text, so I won't cover the topic in this solution, except to say that your best bet is to either use a third party control or print through Word using OLE automation.

Figure 2. Printing ListView Content: The figure shows both a ListView control and the result of printing the contents of that ListView using the project from Part III of this series.

Finally, in Part III of this solution series you'll see how to print tabular data by adding two methods, one for previewing and another for printing items of the ListView control. It's not the most elaborate printing tool, but it's a nice component to add to any interface that uses the ListView control. Figure 2 shows a ListView control displaying a Customer table in detail mode as well as a preview of the control's printout. You can use the same technique to print any type of tabular data, including price lists, invoices, and so on. All you have to do is populate the control, set the column widths and call a method to print or preview the items.

Although VB6's Printer object is convenient, printing in VB has some serious limitations, such as a lack of support for controls for printing and previewing documents (a situation that was rectified in VB.NET). Generating elaborate printouts with VB6 requires a substantial programming effort; therefore most VB6 developers use third party controls for their printouts. But you don't always want to include a large printing library to gain decent print capabilities; therefore, good printing tools have their place in every developer's utilities collection.

You don't have to rely on third-party controls to gain additional printer control in VB. This solution reviews the basics of printing with VB6 and then applies the basic concepts to build some practical tools and utilities.

Thanks for your registration, follow us on our social networks to keep up-to-date