The WinForms Notepad Project: A Little Refactoring (Premium)

Thanks to several useful suggestions from readers, let’s improve our Notepad clone before moving on to add more new features.

And seriously, thanks again for the pointers, everyone. I appreciate this kind of feedback more than you may realize, and I want this series to be as interactive as possible. I’m not a real/professional developer and am learning as I go as well. Thanks.

As you may recall, I tried to implement a semi-reasonable clone of the Notepad dialog that appears when you try to exit with a document with unsaved changes. I assumed that this dialog was something provided not by the system because I couldn’t find anything like it in Visual Studio. But as and jhoersch pointed out, this dialog wasn’t created specifically for Notepad. Instead, it’s a system-provided feature called a Task Dialog that debuted alongside Windows Vista as a replacement for an extension to the capabilities provided by the Message Box dialog.

Task Dialog was implemented as part of the Win32 API and so its native interface is designed for C/C++. But Microsoft provided something called the Windows API Code Pack so that developers could access this and other then-new system features using the .NET Framework in frameworks like Windows Forms and Windows Presentation Foundation (WPF). Sadly, however, the website that Microsoft used to maintain for this project, and the download itself, are no longer available. (Here’s an archived version of the site if you’re curious.)

I wasn’t able to find a copy of the most recent Windows API Code Pack download, nor am I sure that it would even work with Visual Studio 2019, and I’m a bit stunned that Microsoft doesn’t provide a way for non-C/C++ developers to access this necessary control today. But in researching Task Dialog, I did see that some independent developers have worked to create their own C#/.NET wrappers for Task Dialog, though none of these seemed ideal for our needs.

But the more I think about it, the sillier it seems for us to create our own form window to try and replicate the Task Dialog. So we’ll use a simpler workaround that will do for now. And if Microsoft ever does bring Task Dialog to Windows Forms, it will be very easy to swap this out for the real thing.

The workaround, suggested by tjernst, is to use a Message Box. In doing so, all we’ll really lose is the layout/formatting that we’d get with a real Task Dialog, since Notepad only used some basic Task Dialog features. The MessageBox object in Windows Forms lets us throw up a modal dialog that displays the application name in its title bar, a message, and then provide three buttons---in this case, OK, No, and Cancel---that map very closely to the three buttons---Save, Don’t Save, and Cancel---that Notepad’s Task Dialog displays. It’s an acceptable compromise. (And it also lets us use standard return values, as Jordan Meyer had recommended; that was something we could have done with our home-spun Task Dialog...

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