The WinForms Notepad Project: Page Setup and Printing (Premium)

After months of work on .NETpad’s printing functionality, I think I’ve finally arrived at a solution that works. By which I mean Charles Petzold figured it out using C# 18 years ago and I converted his code to Visual Basic, making only minor changes.

Which, honestly, is a small achievement in its own right. But rather than take any credit for this work, let me instead explain how you can add printing support that actually works to .NETpad.

I first added basic but mostly broken printing support to the app in The WinForms Notepad Project: New Window, Printing Basics (Premium) over a month ago, assuming I could improve it over time and make it right. But that proved difficult, in part because the way that Windows Forms handles printing is so crazy and incomplete. And in part because I simply wasn’t up to the challenge.

In that part of this series, I had you add three components to the project, a PrintDocument, a PageSetupDialog, and a PrintDialog. We will still need those. But all of the code we previously added the project needs to go. That includes the following two lines of code in the Form1_Load event handler:
PageSetupDialog1.PageSettings = New Printing.PageSettings
PageSetupDialog1.PrinterSettings = New Printing.PrinterSettings
And it includes the following event handlers and methods. Just delete all of them from Form1.vb.

PageSetupToolStripMenuItem_Click
PrintToolStripMenuItem_Click
PrintDocument1_PrintPage

Now, let’s move forward.

First, open Form1.vb and scroll to the top if necessary, to the area directly inside the top of the Form1 declaration where the various properties (which, stupidly, I still think of as global variables) are. You’re going to add four more properties related to printing:
Public Property PrintText As String
Public Property StartPage As Integer
Public Property NumPages As Integer
Public Property PageNumber As Integer
Now, open Form1.vb [Design] and select PageSetupDialog in the list of components below the form. In the Properties pane, change its Document property to PrintDocument1. Then, select PrintDialog1 in the list of components below the form. And in the Properties pane, change its Document property to PrintDocument1 as well.

This was a simple change I had figured out myself: We were previously writing code to ensure that changes made by the user in the PageSetupDialog made their way to the PrintDocument and then to the printed page (via the PrintDialog). But by linking them in this way, there’s no need to write or maintain that code.

I had also figured out that we didn’t need to manually create an event handler for the PrintDocument’s PrintPage event (which I had called PrintPageHandler). It’s not easy to find, but Visual Basic will create an empty event handler for us. To do this, open Form1.vb and, open the second (middle) drop-down above the code editor, and then locate and select PrintDocument1.

Then, in the third (rightmost) drop-down, locate...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC