The UWP Notepad Project (Redux): New, Save, and Save As (Premium)

With File Open and the Save prompt out of the way, we can move forward to the other app commands that involve file operations. This includes the New button, which needs to perform the same checks as we did with File Open, and the Save button, which will call our now-empty Save() or SaveAs() methods as needed.

Let’s start with the New button.

As with the Open button, we can’t just reset everything and present a new empty document to the user; we need to make sure that there isn’t something that needs to be saved first. And that means we can simply use the same basic code structure that we created for OpenButton_Click in NewButton_Click. But NewButton_Click doesn’t exist yet, so create that now. (I assume you know how by now.)

In NewButton_Click, let’s add that same check we used in OpenButton_Click:
private async void NewButton_Click(object sender, RoutedEventArgs e)
{
    bool result = await DisplaySavePrompt();
    if (result == true)
    {

    }
}
Note that you’ll need to add the async keyword to the top line as shown since we’re using await inside the event handler.

DisplaySavePrompt() determines whether anything needs to be saved. If so, it prompts the user accordingly. If the user chooses “Save” or “Don’t Save,” it returns true, indicating that we can move forward. But if they choose “Cancel,” it returns false, so we don’t do anything, essentially ignoring the original “New” request. If nothing needs to be saved, DisplaySavePrompt() also returns new, so we can move forward.

Inside of that if block, we just need to add some code that resets a few global variables to their default values and changes the app title bar text accordingly. So add the following there and try not to freak out about my inconsistent variable naming style as I just did:
TextBox1.Text = "";
file = null;
TextHasChanged = false;
DocumentName = "";
ApplicationView.GetForCurrentView().Title = "Untitled" + AppName;
We also need to add the CTRL + N keyboard shortcut to the New button. So open MainPage.xaml, locate the NewButton tag, and edit it as we did earlier for OpenButton to include a similar set of keyboard accelerator tags. When you done, the whole line will look like so:
<AppBarButton Icon="Document" Label="New" Name="NewButton" Click="NewButton_Click"><AppBarButton.KeyboardAccelerators><KeyboardAccelerator Modifiers="Control" Key="N" /></AppBarButton.KeyboardAccelerators></AppBarButton>
You can run and test this now, by making a change to a blank document and clicking New, or by opening a document and making a change and then clicking new (or typing CTRL + N). The goal is to display the Save prompt when it’s necessary; remember that DisplaySavePrompt() calls Save() or SaveAs() as needed, but we haven’t written those methods yet, so nothing will actually be saved yet.

OK, let’s head on to the Save button next.

First, create SaveButton_Click. When ...

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