The WPF Notepad Project: Font Dialog (Premium)

When I first started coding the WPF version of .NETpad, I was frustrated by how it turned tasks that were simple in Windows Forms into complex if not impossible tasks. Those things still frustrate me sometimes, but I’ve also come to embrace some of the unique features that make WPF vastly superior, at least in some ways, to WinForms. Key among those features is XAML, the declarative XML-based language that Microsoft created to describe user interfaces.

We used XAML early on in this series to construct the main application window for .NETpad, which is, by nature very simple. But we’ll also use XAML to various build some custom dialogs, starting with the Font dialog. Among other things, these custom dialogs will give us a chance to install .NETpad with a bit more of a modern flavor. Take it a step beyond being a basic desktop application.

I’m happy about that. One of my goals with the WPF version of .NETpad is to bridge past with the present, and so all of the windows we’re creating in WPF purposefully embrace this framework’s resolution independence and display scaling capabilities. And in the custom dialogs, the controls I’m using, like buttons, are bigger than what you’d typically see in a legacy desktop application and are perhaps a bit more similarly sized to controls in mobile Universal Windows Platform (UWP) apps. That’s by design.

Anyway. Let’s get started.

The first thing we need to do is add a new window to the application. You can do so by right-clicking the project name in Solution Explorer and choosing Add > New Item and the looking around in the resulting Add New Item window, but it’s quicker to just choose Add > Windows (WPF) since that will auto-select the right option in that same window.

Rename the window to FontDialog.xaml and click Add. When you do, the new window opens up in Visual Studio in a split view with both Design and XAML panes.

Before messing with the new window, let’s handle the event that displays it: The user clicking Format > Font in the main application window. In MainWindow.xaml, locate and select the tag for the FontMenu menu item. Then, switch the Properties pane to display Events and double-click next to Click to create an empty FontMenu_Click event handler. (You may have done that in an earlier article; if so, just locate FontMenu_Click in MainWindow.xaml.cs.)

We need to perform three basic tasks in this event handler, so let’s add the following comments inside FontMenu_Click to explain what we’ll be doing:
// Save settings so that the Font dialog has the correct font information

// With the settings saved, display the Font dialog

// When the dialog is closed, change the font settings if needed

Under the first comment, add this single line of code:

SaveSettings();
Yes, we could write code that only saves the font-related settings, but we have so few settings, I don’t think it’s worth worrying about.

Under the second commen...

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