The WinForms Notepad Project: New Window, Printing Basics (Premium)

With our Notepad clone getting more mature, the remaining commands are, by nature, the most difficult to implement. So it’s natural that things are slowing down: Some of these features are proving so difficult, they hurt my head. Again, I’m not a professional developer, so some of these topics are straining my abilities.

But that’s fine, and it's expected. So let’s add a few more features.

(Note: I keep promising to work on some fit and finish issues, including some bugs that readers have noticed. I will definitely get to those. But like Microsoft, I just can’t resist the alure of adding new things before fixing code I've already written. Ironic, right?)
File > New Window
I had forgotten about the File > New Window command, which opens a new instance of the application. Indeed, I was considering adding this as a feature that Notepad doesn’t offer. But it does. Ahem.

Anyway, doing this requires just a single line of code, but I had to use Google to figure it out. We need two pieces of information here: The code used to start an EXE file and a way to find the full file name (including path) of our running application.

The first is simple enough: The command Process.Start() can be used to start an EXE file; you just need to pass it the full file name of the EXE (including the path). But I would never have guessed how to get that file name: The code is System.Reflection.Assembly.GetEntryAssembly().Location, which returns a string.

To add this code to the project, create a new menu item below File > New called New &Window and change its ShortcutKeys property to CTRL + SHIFT + N.

Then, double-click that new menu item to open its Click event handler. Add the following line of code:
Process.Start(System.Reflection.Assembly.GetEntryAssembly().Location)
This works, but the new instance of the application window appears directly over the previous instance. I assumed that the real Notepad would cascade new application windows at some kind of an offset, but as it turns out, it behaves the same way as our application. So I will leave that alone for now, but will look into offsetting each new application window a bit from the one underneath it. (This should be doable, as you can pass command line parameters to the EXE run by Process.Start and we could pass the offset and then look for it when the application starts.)
Basic printing
Notepad offers two printing-related commands in the File menu: Page Setup and Print. And I gotta tell you: I’ve struggled with these commands. A lot.

This should be straightforward. Printing, after all, was actually pretty easy in classic Visual Basic, and you’d think that it would get even better with the move to Windows Forms and Visual Basic.NET. But it didn’t. In fact, as I researched this topic, I discovered a 15+-year-old litany of complaints from developers better than me who tried and failed to get printing to work properly in VB.NET.

In a weird coincidence, ...

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