Loading, please wait...

A to Z Full Forms and Acronyms

How to create Notepad in Windows Forms using C#

This article shows how you can build your own Notepad in Visual studio Using WinForms.

Making Notepad in Windows Forms:

Step 1: Open Visual Studio and Create a new project( Windows Form Application). Give it a Suitable name(Here we have named it as “NotePad1”).

Step 2: Click on the Create button. You will come across a Form as given below:

Step 3: Change the name of the form from its properties. This will be displayed on the top of the Notepad as its heading.

From Toolbox, select menu strip and place it on the top in the form area. In MenuStrip, you can provide the names of the various options that you want in your notepad. We are adding File, Edit, and Format options in the menu. You can add more as per your choice.
Step 4:  Now in the options provided in the MenuBar, we need to have a dialog box that will open up as the user clicks on the File, Edit, or Font option. Therefore, we will provide further options for them.

  • For FILE we will add options as follows:
  • Similarly, for Edit and Font, we will add the options as shown below:

Step 5: Now we need to add a RichTextBox control from the toolbox in the form so that the user may enter input in that field. Here, we are not adding a simple textbox because it can basically take single line input whereas RichTextBox provides more control over styling the text.

  • From the properties of the RichTextBox, set anchor property to top, bottom, left, and right and dock property as fill so that the RichTextBox field is spread all over and covers the notepad screen area completely.
  • Anchor Property:

  • Dock property:

Step 6: Now We need to Provide functionality to the various options in the dialog box. For options like Save, Open, Font, and Color, we need some special controls from the toolbox.

  • For save: We need to add saveFileDialog control from the toolbox.
  • For open: We need to add openFileDialog control from the toolbox.
  • For font: We need to add fontDialog control from the toolbox.
  • For Color: We need to add colorDialog control from the toolbox.

 

  • The functionality to each option can be specified in the coding section that you will come across as you double click each option.

The code for the whole notepad is given below:

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;
using System.IO;                        
// assembly for file handling//

namespace NotePad1
{

    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

// to open a new file //

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

// to open a file//

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(openFileDialog1.ShowDialog()== DialogResult.OK)
            {
                richTextBox1.Text = File.ReadAllText(openFileDialog1.FileName);
            }
        }

// to save a file //

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.DefaultExt = ".txt";
            saveFileDialog1.Filter = "Text File|*.txt|PDF file|*.pdf|Word File|*.doc";
            DialogResult dr = saveFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
            }
        }

// to exit and close notepad//

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

// to perform undo operation on the text//

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Undo();
        }

// to perform redo operation//

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Redo();
        }

// to cut some data from the textbox //

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

// to copy some data in the textbox //

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();
        }

// to paste some copied data//

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();
        }

// to select all the text in the text field //

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }

// to display date and time //

        private void dateTimeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = System.DateTime.Now.ToString();
        }

// for font options //

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Font = fontDialog1.Font;
            }
        }

  // for providing color to the text //

        private void colorToolStripMenuItem_Click(object sender, EventArgs e)        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.ForeColor = colorDialog1.Color;
            }
        }
    }
}

Your own notepad is ready:

A to Z Full Forms and Acronyms