During the printing (or preview) process, the
PrintPage event handler gets called once for each output page. But here's the tricky thing: when the
PrintPage handler was called the first time, it was not to print "page 1" of the document, but to print "the first page in need of printing," whatever its page number. Search all you want through the properties of the
PrintDocument class, but you will never find a
PageNumber property. The
PrintDocument class does not know about the page numbers in your document, and—despite all of the nice things it does for you—it does not care. All it knows is that you have a bunch of pages to print, and it will call your
PrintPage event handler until you say "enough!"
If you turn back to Figure 20-3, you'll see that the Print dialog includes a Page Range section, although most of its controls are disabled by default. The
PrintDialog control includes three Boolean properties that let you enable specific controls in that section:
AllowSelection. Setting any of these properties to
True enables the matching option control. Later, after the user has made a choice, you can query the
PrinterSettings.PrintRange property to determine which choice it is.
Let's add code that enables page range selection. We'll still limit the allowed pages to just those numbered one to five, but the user will be able to choose a subrange within that set. Return to the