Adding a print preview interface is so easy, you should probably ask your boss for a really hard project to do first, and then come back when you're worn out. Let's build on our simple number-printing application, adding a new Button control named ActPreview. We will also add a PrintPreviewDialog control named UserPreview. Once these are in place, add the following preview button Click event handler.
Private Sub ActPreview_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ActPreview.Click ' ----- Display a preview of the document. UserPreview.Document = CountingDoc UserPreview.ShowDialog() End Sub
Hey, that's even simpler than the code that initiates printing to a real printer, even though print preview technology seems to be more complex than plain printing. There almost ought to be a law against code that simple. Fortunately, there's not. Figure 19-7 shows the preview window, after using the two-pages-at-once toolbar button.
Figure 19-7. The preview displays multiple pages at once with no extra effort on our part
Let's dwell just a little longer on how simple that code was. I can accept that the PrintPreviewDialog class includes a lot of amazing code for previewing printed output. But the remarkable part of the code is that we didn't have to rewrite the custom GDI+ drawing logic. The same set of GDI+ statements now drives the preview display and the actual output. All we had to do was assign the PrintDocument object to the correct dialog control.