Building a Notepad in C#

The best way to learn a programming language is by trying out different projects. A nice project to start with is building a simple notepad. The notepad in this guide has the functionalities of opening and saving files, editing text and several other basic functionalities.

Step 1: Adding the Menu

The first thing we will be doing after starting a new forms applicaton project in Visual Studio, is adding a menu. To do this you have to go to “Toolbox” and then select “MenuStrip”. I you can’t find the Toolbox or have accidentally removed it. You can access it by going to “View” were it will be listed, or simply enter ctrl+alt+x.

After selecting the MenuStrip add it to the upper most part of the empty form, the result should look like the figure below:

After adding the MenuStrip we add the different sections in the menu we want the notepad application to have. You can simply do this by clicking on the MenuStrip and typing in the name of the section. For now we add a “File” section and an “Edit”section.

Then we add all options in every section. The File section has the options of opening, saving and closing the notepad. The Edit section has the options of undoing, redoing, copying, cutting, pasting and deleting text.

After adding all the sections and options the menu looks like this:

Step 2: Adding the Place to Edit Text

The next step is adding the place to write or edit text in. To do this you have to go back to the Toolbox and select “RichTextBox”. Add this part under the MenuStrip:

Notice that just by simply adding this part, the size of it won’t change according to the size of the window it is in. In order to have this functionality you have to click on the small arrow shown in the figure below and select “Dock in Parent Container”.

Step 3: Adding the Code

Step 3 is the most difficult part of this project, because you can easily lose track of all the code that has to be added. That is why this part is devided in subsections. When you are stuck simply retrace your steps and check the code in earlier sections. At the end of this guide you can find the complete code.

Step 3.1: The File Section of the Menu

Earlier we added the options of opening, saving and closing a text document. In this part of the guide we add the underlying code. The code to open and to save text in the notepad is the most complex code of this guide. All other functionalities are very much straightforward.

Open

Go to the “Open” option you added earlier and dubble click it to acces the code. You have to first add this code to the upper part of the overall code:

using System.IO;

Go to the code of the specific “Open” option in the menu, add “async” between private and void to the first line of code of this specific option. The result shoud look like this:

private async void OpenToolStripMenuItem_Click(object sender, EventArgs e)
{

Then initialize an instance of the OpenFileDialog and the StreamReader class, with an if statement between the brackets:

 OpenFileDialog openFileDialog1 = new OpenFileDialog();
 StreamReader streamReader1 = new StreamReader(openFileDialog1.FileName);

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                richTextBox1.Text = await streamReader1.ReadToEndAsync();  
                }
           

Save

To save a file the first couple of lines are similar. Double click “Save” in the menu and initialize an instance of the SaveFileDialog between the brackets and add an if statement under it:

 SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
richTextBox1.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
            }

From here on the code for the other options is very straightforward. Dubble click every option in the menu before adding its code.

For the option of closing the notepad you need to add the following code:

this.Close();  

Another way to do this, is using this code instead:

Application.Exit();

After having added all the previous code you should be able to open text files, save text and close the notepad from the menu. Execute the application through “Start” to check if these functionalities work.

Go to the end of this guide to see a complete overview of the code.

Step 3.2: The Editing Section

The editing section contains the options to undo and redo text and the abilities to copy, cut, paste and delete text.

Undo

To undo text through the option in the menu add the following code:

richTextBox1.Undo();

Redo

To redo text the following code is needed:

richTextBox1.Redo();

Copy

Other options are copying, cutting, pasting and deleting text. The code for these options is as simple as the previous options. To copy text add the following code:

richTextBox1.Copy();

Cut

To cut text add the following code:

richTextBox1.Cut();

Paste

To paste text add the following code:

richTextBox1.Paste();

Delete

The code to delete text is slightly different:

richTextBox1.SelectedText = "";

Step 4: Debugging

The last step of building the notepad is reviewing the code and identifying possible bugs. Below you can find the complete code, which you can compare with you own code.