424 Part III Designing the User Interface
Printing Multipage Text Files
The printing techniques that you’ve just learned are useful for simple text documents, but
they have a few important limitations. First, the method I used doesn’t allow for long lines—
in other words, text that extends beyond the right margin. Unlike the text box object, the
PrintDocument object doesn’t automatically wrap lines when they reach the edge of the
paper. If you have files that don’t contain carriage returns at the end of lines, you’ll need to
write the code that handles these long lines.
The second limitation is that the Print Text program can’t print more than one page of text.
Indeed, it doesn’t even understand what a page of text is—the printing procedure simply
sends the text to the default printer. If the text block is too long to fit on a single page, the
additional text won’t be printed. To handle multipage printouts, you need to create a virtual
page of text called the PrintPage and then add text to it until the page is full. When the page
is full, it is sent to the printer, and this process continues until there is no more text to print.
At that point, the print job ends.
If fixing these two limitations sounds complicated, don’t despair yet—there are a few
handy mechanisms that help you create virtual text pages in Visual Basic and help you print
text files with long lines and several pages of text. The first mechanism is the PrintPage
event, which occurs when a page is printed. PrintPage receives an argument of the type
PrintPageEventArgs, which provides you with the dimensions and characteristics of the
current printer page. Another mechanism is the Graphics.MeasureString method. The
MeasureString method can be used to determine how many characters and lines can fit
in a rectangular area of the page. By using these mechanisms and others, it’s relatively
straightforward to construct procedures that process multipage print jobs.
Complete the following steps to build a program named Print File that opens text files
of any length and prints them. The Print File program also demonstrates how to use the
RichTextBox, PrintDialog, and OpenFileDialog controls. The RichTextBox control is a more
robust version of the TextBox control you just used to display text. The PrintDialog control
displays a standard Print dialog box so that you can specify various print settings. The
OpenFileDialog control lets you select a text file for printing. (You used OpenFileDialog
in Chapter 4, “Working with Menus, Toolbars, and Dialog Boxes .”)
Manage print requests with RichTextBox, OpenFileDialog, and PrintDialog controls
- Click the Close Project command on the File menu, and then create a new Windows
Forms Application project named My Print File.
A blank form opens. - Use the Button control in the Toolbox to draw two buttons in the upper-left corner of
the form.
This program has a simple user interface, but the printing techniques you’ll learn are
easily adaptable to much more complex solutions.