1,066

How to create a Notepad application

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

In this article, I have explained the procedure to create a notepad application.

Introduction

This is a simple window application, which explains how to create a Notepad application.

Description

To create a notepad application, the primary requirements are to place a MenuBar control and a textbox control. A menubar is a collection of menu’s and a menu is a collection of menu items.

To create a simple notepad application, create a new project with a name NotepadApp and design the window as shown below. 

According to your requirement, you can add the submenus in a menu as shown in the above image.
In my application, I have added File, Edit, Format, Help menus in a menu control.
Under File menu: New, Open, Save, Print, Exit.
Under Edit menu: Cut, Copy, Paste, SelectAll.
Under Format menu: Font, Color.
Under Help menu: AboutUs.

Set the textbox property as : Name = txtContent, Dock = Fill and Multiline = true.

To work with the above applicaion, 7 controls are required that is

•  OpenFileDialog control : This control is used here to print an open dialog box.
•  SaveFileDialog control : This control is used here to print a file dialog box.
•  PrintDialog control : This control is used here to print a print dialog box.
•  FontDialog control : This control is used here to print a font dialog box.
•  ColorDialog control : This control is used here to print a color dialog box.
•  MenuStrip control : This is used here to add different menu items.

So to add the above controls, Go to the tool box and add accordingly the controls as mentioned. Below is the image.

Code to work with above Requirement

 

using

 System;

using

 System.Collections.Generic;

using

 System.ComponentModel;

using

 System.Data;

using

 System.Drawing;

using

 System.Linq;

using

 System.Text;

using

 System.Windows.Forms;

using

 System.IO;

 

 

namespace

 NotepadApp

{

publicpartialclassForm1 : Form

{

publicForm1()

{

 

InitializeComponent();

 

}

privatevoid Form1_Load(object sender, EventArgs e)

{

 

}

privatevoid newToolStripMenuItem_Click(object sender, EventArgs e)

{

 

txtContent.Text =

"";

}

privatevoid openToolStripMenuItem_Click(object sender, EventArgs e)

{

 

openFileDialog1.ShowDialog();

 

//Now it show open dialog box.//Read the filename selected by the user with in open file dialog box.string fName = openFileDialog1.FileName;//Read the data by using StreamReader classStreamReader sr = newStreamReader(fName);

txtContent.Text = sr.ReadToEnd();

 

sr.Close();

 

}

privatevoid saveToolStripMenuItem_Click(object sender, EventArgs e)

{

 

saveFileDialog1.ShowDialog();

 

string fName = saveFileDialog1.FileName;StreamWriter sw = newStreamWriter(fName);

sw.Write(txtContent.Text);

 

sw.Flush();

 

sw.Close();

 

}

privatevoid printToolStripMenuItem_Click(object sender, EventArgs e)

{

 

printDialog1.ShowDialog();

 

}

privatevoid exitToolStripMenuItem_Click(object sender, EventArgs e)

{

Application.Exit();

}

privatevoid cutToolStripMenuItem_Click(object sender, EventArgs e)

{

 

txtContent.Cut();

 

}

privatevoid copyToolStripMenuItem_Click(object sender, EventArgs e)

{

 

      txtContent.Copy();

 

}

privatevoid pasteToolStripMenuItem_Click(object sender, EventArgs e)

{

 

      txtContent.Paste();

 

}

privatevoid selectAllToolStripMenuItem_Click(object sender, EventArgs e)

{

 

      txtContent.SelectAll();

 

}

privatevoid fontToolStripMenuItem_Click(object sender, EventArgs e)

{

 

fontDialog1.ShowDialog();

txtContent.Font = fontDialog1.Font;

 

}

privatevoid colorToolStripMenuItem_Click(object sender, EventArgs e)

{

 

colorDialog1.ShowDialog();

txtContent.ForeColor = colorDialog1.Color;

 

}

privatevoid aboutUsToolStripMenuItem_Click(object sender, EventArgs e)

{

//create a form dynamically which displays the information about the company.Form f = newForm();Label ll = newLabel();

ll.Text =

"Write information about the company";

ll.Font =

newFont("Arial", 20);

ll.Dock =

DockStyle.Fill;

f.Controls.Add(ll);

 

f.Show();

 

}

 

}

 

}

 

Here I have used StreamReader class to read the data present with in the specified file. To work with this, we need to import the “System.IO” namespace.

The output window will look like as below image.

Suppose you want to open an existing file then click File menu>> click Open. It will look like as below image.

Conclusion

Hope you will enjoy by reading this article.

prajapat