A Project on How to Create a Notepad kind of Application

Raj.Trivedi
Posted by in C# category on for Beginner level | Points: 250 | Views : 16982 red flag
Rating: 5 out of 5  
 2 vote(s)

In this article we will see how we can create a simple app like notepad in Windows Application

Introduction

This is a Windows Application which resembles notepad. We can implement functionality such
File Menu,Edit Menu,Format Menu.

  • The File menu has menu options New,Open,Save,Print Preview,Print,Exit.
  • The Edit menu has Cut,Copy,Paste and Undo
  • The Format Menu has Font and Page Setup

Objective

To Create a Application such as Notepad.

Using the code


Open MS VISUAL STUDIO 2010 -> File -> New Project -> Windows Application -> Right Click on the Solution and Add a Windows Form.

Go to the properties of Windows form and set its IsMDiContainer Property to True.

Now go to the Toolbox and Drag and Drop a Rich TextBox Control on the Form.

Now go to the Tool box and drag and Drop a MenuStrip on the top portion of the form.

Now click on the  Menu Strip and Type File on the First Position.This will be called menu strip items.

Now Under the File Menu Item add the following items one below another as shown  in the figure.

 
File Menu




Now just beside the File Menu Item add another Menu Item on the menu Tool Strip as EDIT as seen in the above Image.

Once you have added the MenuStrip Item now start adding Menu Items as we did it for File Menu.Below is the Image for Edit Menu.

Edit Menu

 
Now just beside the Edit Item add another Menu Item on the menu Tool Strip as Format as seen in the above Image.

Once you have added the MenuStrip Item now start adding Menu Items as we did it for Edit Menu.Below is the Image for Format Menu as shown in the below image

Format Menu




Now from the Tool box just drag the following controls from the Tool Box.

  1. Save Dialog Box :- This will help to save the text written in RichTextBox as .txt file. 
  2. Open File Dialog Box :- This will help to open the the Notepad files on our computer and read the content in the rich text box of our form
  3. Font Dialog Box :- This will help us to change the font of the text in the Rich Text box.
  4. Page Set up Dialog box :- This will help us to set the page up for printing.
  5. printDocument :- This will help us to print our text that we have written it in Rich text box
  6. Print Dialog 1 :- This will call the Print Confirmation box and help us to choose the printer that is connected to the system
  7. PrintPreviewDialogBox :- To Preview what we are going to print.

Check the Snapshot to view where these tools will be placed

 



Namespaces to add

using System.IO;
using System.Drawing.Printing;

These namespaces are to be added to use the Print and Print View Functionalities and reading a Text file.
// Code for the Entire App
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;
using System.Drawing.Printing;

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

        // This will close the application if you click on exit in the File Menu
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to Exit", "Quit", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        // This will clear the rich text box.
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
        }

        //This will allow to save the text that we have typed in the Rich Text Box in a .txt file
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "unknown.txt";
            saveFileDialog1.Filter = "text file(*.txt)|*.txt|All files(*.*)|*.*"; // to save the file as notepad.
            if (saveFileDialog1.ShowDialog()== DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
                {
                    sw.WriteLine(richTextBox1.Text);
                }
            }
        }

        // This will open the Open dialog box and allow us to select the notepad file and read the content in the Rich Text box.

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName);
                richTextBox1.Text = sr.ReadToEnd();
                sr.Close();
            }
        }

        // This will allow us to copy the text written in the text box.This will only work after you select the text in the Rich text box
        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectionLength > 0) richTextBox1.Copy();
        }

        // This will allow us to paste the text written in the text box.This will only work after you select the text in the Rich text box

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

        // This will allow us to cut the text written in the text box.This will only work after you select the text in the Rich text box
        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectedText != "") richTextBox1.Cut();
        }

        // This will allow us to undo the changes in the text box.
        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.CanUndo == true) richTextBox1.Undo();
        }


        // This will allow us to open the font dialog box and change the font in the for the text in the Rich Text box.The font will only change when you select the text
        // and select the font from the font dialog box.
        
        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.SelectedText != "")
            {
                richTextBox1.Select();
                if (fontDialog1.ShowDialog() == DialogResult.OK)
                {
                    richTextBox1.Font = fontDialog1.Font;
                }
            }
        }

        // This will open page setup dialog box and allow you to set up the page settings

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PageSetupDialog setupDlg = new PageSetupDialog();
            setupDlg.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
            setupDlg.PageSettings = new System.Drawing.Printing.PageSettings();
            setupDlg.EnableMetric = false;
            setupDlg.ShowDialog();
            System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
         
        }

        // This will call the print dialog box to select the printer.

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {

            printDialog1.Document = printDocument;

            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument.Print();
            }
        }



        // This will allow us to see print preview before printing.
        
        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument;
            printPreviewDialog1.ShowDialog();

            
        }


        // This will send the Rich Text Box data to print.
        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
            e.Graphics.PageUnit = GraphicsUnit.Inch;  
        }

        

    }
}


Conclusion

I hope this will help the dot net programmers......
Sharing is Caring..... 



Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)