How to create a Notepad application

Tripati_tutu
Posted by in C# category on for Intermediate level | Points: 250 | Views : 43537 red flag

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

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

}

private void newToolStripMenuItem_Click(object sender, EventArgs e)

{

txtContent.Text = "";

}

private void 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 class

StreamReader sr = new StreamReader(fName);

txtContent.Text = sr.ReadToEnd();

sr.Close();

}

private void saveToolStripMenuItem_Click(object sender, EventArgs e)

{

saveFileDialog1.ShowDialog();

string fName = saveFileDialog1.FileName;

StreamWriter sw = new StreamWriter(fName);

sw.Write(txtContent.Text);

sw.Flush();

sw.Close();

}

private void printToolStripMenuItem_Click(object sender, EventArgs e)

{

printDialog1.ShowDialog();

}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)

{

Application.Exit();

}

private void cutToolStripMenuItem_Click(object sender, EventArgs e)

{

txtContent.Cut();

}

private void copyToolStripMenuItem_Click(object sender, EventArgs e)

{

      txtContent.Copy();

}

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)

{

      txtContent.Paste();

}

private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)

{

      txtContent.SelectAll();

}

private void fontToolStripMenuItem_Click(object sender, EventArgs e)

{

fontDialog1.ShowDialog();

txtContent.Font = fontDialog1.Font;

}

private void colorToolStripMenuItem_Click(object sender, EventArgs e)

{

colorDialog1.ShowDialog();

txtContent.ForeColor = colorDialog1.Color;

}

private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)

{

//create a form dynamically which displays the information about the company.

Form f = new Form();

Label ll = new Label();

ll.Text = "Write information about the company";

ll.Font = new Font("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.

Page copy protected against web site content infringement by Copyscape

About the Author

Tripati_tutu
Full Name: K.Tripati Balaji Subudhi
Member Level: Bronze
Member Status: Member
Member Since: 9/20/2010 5:58:39 AM
Country: India

http://www.dotnetfunda.com/profile/Tripati_tutu.aspx

Login to vote for this post.

Comments or Responses

Posted by: Karthikanbarasan on: 2/14/2011 | Points: 25
Simple and nice!!!
Posted by: Madhu.b.rokkam on: 2/14/2011 | Points: 25
Simple ! Just you can add a filter to show only txt files in the open dialog. :)
Posted by: Tripati_tutu on: 2/14/2011 | Points: 25
Thanks for your nice responses.

Tripati
Posted by: A4u6178 on: 5/21/2011 | Points: 25

Very nice article

Login to post response

Comment using Facebook(Author doesn't get notification)