The WinForms Notepad Project: File Save and a Stub Status Bar (Premium)

As this project has evolved, I’ve been testing out individual features I intend to add to the Notepad clone in isolation. On the Surface Pro 7, which I’ve been using most recently, for example, there are Visual Studio projects related to saving a text file, Task Dialog (which I’ve shelved for now), the status bar (not yet implemented), zoom (not yet implemented) and an About Box (also not implemented). When these features seem to work correctly, I add them to the master version of the project, which is on the Surface Book 2.

Obviously, manipulating text files is job one for any Notepad clone. I originally implemented the opening half of that equation using the StreamReader object, but changed to a more efficient method using the File object thanks to a reader suggestion. So I scrapped my original StreamReader-based approach to saving a text file and switched to the File object there as well.

If you think about how Notepad works, the Save and Save As operations come up quite a bit, and in different ways. If you start with a blank document and type CTRL + S (or choose File > Save), you’ll be prompted to provide a filename and path using a system Save As dialog box. But if you’ve previously saved the document, this will just save it again with no prompting. Likewise, you’ll be prompted to save if you have an unsaved document (or a saved document with unsaved changes) and try to close the application. Save As has its own permutations as well. And we have to try and address all of them.

Let’s start with the menu items related to saving. Our application currently has only two items in the File menu, Open and Exit, and if you look at the real Notepad’s File menu, you’ll see that it has both Save and Save As and that each has a keyboard accelerator. So let’s add those first.

Adding menu items inside of a menu is a little bit tedious, especially in this case. In Visual Studio, display Form1.vb [Design], open the File menu in our application, and then select and right-click the separator above the Exit item. Then, select Insert > MenuItem.

A new menu item named ToolStripMenuItem1 will appear below Open and above the separator.

Now, repeat the same process again to add a second new menu item, which will be named ToolStripMenuItem2 and will appear under ToolStripMenuItem1.

Selecting each new menu item in turn, use the Properties pane to change the name of ToolStripMenuItem1 to SaveToolStripMenuItem, and change ToolStripMenuItem2 to SaveAsToolStripMenuItem. You will need to change the Text property of each (to “&Save” and “Save &As…”, respective, with no quotes). And you will need to change the ShorcutKeys property for each: CTRL + S for SaveToolStripMenuItem and CTRL + SHIFT + S for SaveAsToolStripMenuItem.

We also need to add a SaveFileDialog object to our project. With Form1.vb [Design] displayed, find SaveFileDialog in the Toolbox and double-click it to add it.

W...

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