Making Notepad Application Using C# in Visual Studio 2013

Notepad is a familiar and famous and basic text editor program that exists on Windows computer with very basic text editing capability.

We can make our own notepad application for Windows operating system using C# programming. The programming techniques, code and screenshots used in this tutorial are in compliance with the latest versions of the following tools and technologies. Here they are,
  • C# 5.0
  • Visual Studio 2013
  • .Net Framework 4.5
Let’s get started,

Go to File > New > Project > select C# > Windows Forms Application. This creates a new form(Form1) by default in the workspace.

Drag and drop the MenuStrip from Toolbox > Menus & Toolbars on to the form. we need to add the MenuItems and Sub-MenuItems in the MenuStrip now.

As we are doing a Notepad application here, we’ll add up File and Edit menus. Under File menu, add New, Edit & Save sub-MenuItems and under Edit, add Cut, Copy & Paste sub-MenuItems. This will make the application look like this,

In next step, we’ll have to add the TabControl to generate the File > New functionality.

Add a TabControl from All Windows Forms tools. By default, it will create a TabControl with two tabPages. This will look something like this,

As we have no need of them while using along with MenuStrip, we’ll remove them by Right-Click on tab page > Delete. After deleting both the tabPages, the TabControl will look something like this,

The TabControl should not be moved or changing its position with the resizing of the window. So, let's dock it to the form. Select TabControl > Press F4(Properties) > Under Layout > set Dock property to Fill. The resultant form after docking of TabControl will look like this,

Now, let’s write the purposeful code for the TabControl, i.e., adding a new tab when clicked on File > New.

Adding ‘New’ functionality

Double-click on the New from File MenuStrip, it will enable the click event and allows us to add our own code. This will look something like this,

Add the following code in the event handler:

TabPage tp = new TabPage("New Document"); //creates a new tab page
RichTextBox rtb = new RichTextBox(); //creates a new richtext box object
rtb.Dock = DockStyle.Fill; //docks rich text box
tp.Controls.Add(rtb); // adds rich text box to the tab page
tabControl1.TabPages.Add(tp); //adds the tab pages to tab control
After adding, it will look something like this,

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
    TabPage tp = new TabPage("New Document"); //creates a new tab page
    RichTextBox rtb = new RichTextBox(); //creates a new richtext box object
    rtb.Dock = DockStyle.Fill; //docks rich text box
    tp.Controls.Add(rtb); // adds rich text box to the tab page
    tabControl1.TabPages.Add(tp); //adds the tab pages to tab control
}
The purpose of adding the lines in the code is briefly explained in single comments are followed.

Adding ‘Edit’ functionality

We are now done with adding a new document for File > New menu option. Let’s also work with the Edit menu options.

There available a readymade RichTextBox control available in C#.NET using which we can implement Cut, Copy and Paste options.

Now, add this following function in your code,

private RichTextBox GetRichTextBox()
{
    RichTextBox rtb = null; //making it initially null
    TabPage tp = tabControl1.SelectedTab; // saves the tab selection status in a tabpage type variable
    if(tp!=null)
      {
        rtb = tp.Controls[0] as RichTextBox; //sets the selected rich text box index as primary
      }
    return rtb;
}
Now, double-click on the Cut sub-MenuItem from File > Cut. I will open into its event handler function. Then write GetRichTextBox().Cut() in it.

Do the same with Copy and Paste options also. Only the methods Copy(), Paste() will replace Cut() from the above line. Then the resultant code will look like this,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Notepad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
        }
        private RichTextBox GetRichTextBox()
        {
            RichTextBox rtb = null; //making it initially null
            TabPage tp = tabControl1.SelectedTab; // saves the tab selection status in a tabpage type variable
            if(tp!=null)
            {
                rtb = tp.Controls[0] as RichTextBox; //sets the selected rich text box index as primary
            }
            return rtb;
        }
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tp = new TabPage("New Document"); //creates a new tab page
            RichTextBox rtb = new RichTextBox(); //creates a new richtext box object
            rtb.Dock = DockStyle.Fill; //docks rich text box
            tp.Controls.Add(rtb); // adds rich text box to the tab page
            tabControl1.TabPages.Add(tp); //adds the tab pages to tab control
        }
        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Cut();
        }
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Copy();
        }
        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Paste();
        }
    }
}
We are done with New, Cut, Copy and Paste options so far. We need to work on Open and Save functionalities now.

Adding ‘Open’ functionality

To open a file into the rich text box, we need to add a OpenFileDialog() that accepts the file from computer and write the code to insert it in the rich text box that we’ve added previously.

Drag and drop the OpenFileDialog()on to the form and then double-click on Open sub-MenuItem from File MenuStrip.  It will open into its event handler function that looks like as shown below,

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
}

Now add the following piece of code in the function. After adding, it will look something like this,

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
   Stream myStream;
   if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
   {
       if((myStream=openFileDialog1.OpenFile())!=null)
        {
           string filename = openFileDialog1.FileName;//copies the path of the file into a variable
           string readfiletext = File.ReadAllText(filename);//reads all the text from the opened file
           TabPage tp = new TabPage("New Document"); //creates a new tab page
           RichTextBox rtb = new RichTextBox(); //creates a new richtext box object
           rtb.Dock = DockStyle.Fill; //docks rich text box
           tp.Controls.Add(rtb); // adds rich text box to the tab page
           tabControl1.TabPages.Add(tp); //adds the tab pages to tab control
           rtb.Text = readfiletext;
         }        
   }
}
Before adding the above code, make sure you have included the following references,

using System.Text;
using System.IO;

Or else, you would get an error while declaring Stream reference.

Run the application and test it by uploading a file. It even accepts XML files when loaded.

Adding ‘Save’ functionality

So, we are successfully done adding Open functionality and the only functionality left to work with is the ‘Save’.

We need to use the SaveFileDialog on TabPages to achieve this. Just dragging and dropping on the form may not help as it creates TabPages only in runtime.

Let’s add it manually.

Double-click on the Save sub-MenuItem from File MenuStrip and it opens into its event handler function.

Adding the code into it will appear like as shown below,

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
   SaveFileDialog savefile=new SaveFileDialog();
   savefile.Filter = "*.txt(textfile)|*.txt";
   if(savefile.ShowDialog()==DialogResult.OK)
   {
      rtb.SaveFile(savefile.FileName, RichTextBoxStreamType.PlainText);
   }
}
That’s it! We’re done with all the basic functionalities we have added. This is how it looks,

Now let’s make it look more beautiful

Functionally, we are done with everything. But when executing, it still shows the title as ‘Form1’ with no favicon and opens in s small window. Let’s fix it by customizing!

Go to Form1.cs Design > Properties > Change the text from Form1 to anything you would like to display on the title bar. I’d choose to display Notepad.

Text: Form1

to

Text: Notepad

Next, let’s change the icon.

Under Window Style, set ShowIcon property to True and choose an icon from your computer for Icon property.

Changing the icon is done, let’s fix the small window startup.

Navigate to Layout properties > Change WindowState from Normal to Maximized. This will open the window maximized when the program is started. Take a look at the following screenshot of how it looks after tweaking.

Hope I covered all those very basic functionalities for a customized Notepad. Checkout how it works finally:


Creating the executable package

So, you have developed your own software program. Let’s create a executable package of it so that everyone else can simply install it on their computers and use it.

Go to Solution Explorer > Right-click on the project > Publish. Go through the wizard and finish the process of creating a installable file of your project.

You can download Amfas Tech’s Notepad from this link to test the application we have developed so far.

Access the source code, make suggestions and modify the code of this project on GitHub.

Feel free to ask me in comments if any support is needed regarding this. I can help you to my best in solving and making it understandable to you.

0/Post a reply/Replies

Previous Post Next Post