The WPF Notepad Project: Open File, TextBox Events, Theming (Premium)

Building on the progress we made last time, let’s add open file functionality, textbox events, and theming support to .NETpad.

First up, opening files...
File > Open
Even with our limited understanding of WPF commands, it should come as no surprise that Open---really, ApplicationCommands.Open---is one of the natively-supported commands. So we’ll implement the File > Open event handler that way (as opposed to using a Click event handler). We’ll also map the CTRL + O keyboard shortcut to call the same Executed command handler if needed.

To get started, let’s add the Command property to the Open menu item in MainWindow.xaml. When you’re done, it should look like so:
<MenuItem Command="Open" Name="OpenMenu" Header="_Open..." />
Next, we need to bind the command to an Executed event handler. We do that in the <Window.CommandBindings> section of the XAML file. In keeping with the loose style I’m using, I’m placing this line of XAML code above the Print command binding in that section (because Open appears above Print in the app’s menu):
<CommandBinding Command="Open" Executed="OpenCommand_Executed" />
Now, we need to create a blank OpenCommand_Executed in MainWindow.xaml.cs. As usual, I am placing this at the bottom of the MainWindow class definition.
private void OpenCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{

}
For our first pass at this functionality, we’ll just add some code to display a standard Open File dialog and, if the user selects a text file, open the contents of that file in .NETpad. In the future, we’ll be adding some more logic here to check whether we need to save whatever text/document is potentially already there. That looks like so:
private void OpenCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog
    {
        Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    };
    if (openFileDialog.ShowDialog() == true)
    {
        try
        {
            TextBox1.Text = File.ReadAllText(openFileDialog.FileName);
            AppWindow.Title = openFileDialog.SafeFileName + " - " + Application.Current.MainWindow.GetType().Assembly.GetName().Name;
            TextHasChanged = false;
            DocumentName = openFileDialog.FileName;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
The OpenFileDialog and File objects will be flagged by Visual Studio, so you’ll need to add includes for Microsoft.Win32 and System.IO, respectively, to clean that up.

This works fine. In fact, even the CTRL + O keyboard shortcut works---and a hint to that effect appears in next to the Open item in the File menu---because Open is a natively supported command. So there’s no more work to do. (Aside fr...

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