The WinForms Notepad Project: Fixes, File New, and Zoom (Premium)

With basic file save capabilities out of the way, let’s do a bit of fit and finish work and add some more new features.

First up, a few improvements to the existing code.
Exception handling
Mojohill mentioned that the SaveToolStripMenuItem_Click event handler was throwing an exception related to permissions and suggested putting the line with the method System.IO.File.WriteAllText in a Try block. He’s right, and I meant to do that originally. So the SaveToolStripMenuItem_Click event handler should now look like so:
If DocumentName <> "" Then
   Try
       System.IO.File.WriteAllText(DocumentName, TextBox1.Text)
       TextHasChanged = False
       Me.Text = Me.Text.Replace("*", "")
   Catch ex As Exception
       MessageBox.Show(ex.Message)
   End Try
Else
   SaveAsToolStripMenuItem_Click(sender, e)
End If
Of course, this won’t solve his problem, and the document still won’t get updated. But it will prevent the application from crashing and will explain why in a message box. For example, when I tried to edit a text file in C:\Windows.old\WINDOWS\SysWOW64 and then save it, I got the following error:

If anyone is getting this message box on the Save command, let me know so we can figure out how to really fix it. And while we’re at it, let’s change all the Catch blocks in Form1.vb to match what we used here, so we can always tell the user what’s wrong.
Form1_Closing event handler
I forgot to add file saving capabilities to the Form1_Closing event handler, which currently looks like this:
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
   If TextHasChanged Then
      Dim SavePrompt As DialogResult = MessageBox.Show("Do you want to save changes?", "NotepadWF", MessageBoxButtons.YesNoCancel)
       If SavePrompt = vbYes Then
           ' Display a Save Dialog here
       ElseIf SavePrompt = vbNo Then
           Application.Exit()
       Else
          e.Cancel = True
      End If
  Else
       Application.Exit()
   End If
End Sub
My bad. But this one is easy to fix: Just replace the commented out line (' Display a Save Dialog here) with:
SaveFileDialog1.ShowDialog()
OK, let’s add some new commands now, too.
File > New
The real Notepad has a File > New command that lets the user start over with a blank document. You already know how to add this menu item to the File menu of our application in the designer: Right-click File > Open and then select Insert and then MenuItem from the context menu that appears. Then, name this new menu item as &New and change its ShortcutKeys property to CTRL + N.

As for its Click event handler, we need to check to see whether there were any pending changes to the document they were already working on and then prompt them to save it if they’d like. If this sounds familiar, it’s be...

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