The fourth installment of our beginning programming project loads a file more efficiently and prompts the user to save when changes are made.
As you may recall, in part 3 of this series, called The WinForms Notepad Project: Icons and Open Files (Premium), I used a StreamReader object to read the contents of a text file; the contents of that file were then loaded into the TextBox that forms the foundation of our Notepad clone. But a reader with the clever user name of CleverUsername suggested I try File.ReadAllText instead.
Well, he is clever.
StreamReader provides a number of methods related to reading lines of information from a text file. The method I used, ReadToEnd, does what it sounds like it does: It reads the entire contents of the text file (as opposed to one character or one line, or whatever).
The ReadAllText method of the File object works similarly. But it does so with less code, which is preferable. For one, it doesn’t require creating a variable every single time the event handler it’s in is called. And for another, it does automatic garbage collection. I had used a Close method to do that manually on the StreamReader object.
So let’s change it.
(The File object, like StreamReader is found in the System.IO namespace. Since we imported that namespace previously, we don’t need to make any changes there.)
Open up Form1.vb and find the Open menu item’s Click event handler. It’s called OpenToolStripMenuItem_Click, but the easiest way to go to it directly is to Form1.vb [Design] in the Visual Studio designer and then double-click the Open menu item under File (in our app window).
Currently, the body of the event handler looks like this:
Try
  Dim Contents As New StreamReader(OpenFileDialog1.FileName)
  TextBox1.Text = Contents.ReadToEnd
  Me.Text = OpenFileDialog1.SafeFileName + "- NotepadWF"
  Contents.Close()
Catch ex As Exception
  MessageBox.Show("Something happened.")
End Try
We’re going to delete three of the lines of code between Try and Catch. Leave only the line that reads:
Me.Text = OpenFileDialog1.SafeFileName + "- NotepadWF"
Now, above that line, add the following single line of code:
TextBox1.Text = File.ReadAllText(OpenFileDialog1.FileName)
To be clear, the body of the Click event handler for the Open menu item should now read as follows:
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
  Try
      TextBox1.Text = File.ReadAllText(OpenFileDialog1.FileName)
      Me.Text = OpenFileDialog1.SafeFileName + "- NotepadWF"
  Catch ex As Exception
      MessageBox.Show("Something happened.")
  End Try
End If
If you run the application, you’ll see that it works as before. But the new code is shorter and more efficient. That’s a win. Thanks again, CleverUsername. (And I should have said this explicitly before, but I’m learning along with all of you. If you have any suggestions about making this application o...
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.