Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
2.20/5 (3 votes)
See more:
I'm learnin c# and I want to create my notepad using c#
I writing this code
C#
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 NotePad
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {   // This is where the code for the button click of "New" is going to go.
            SaveFileDialog sfd = new SaveFileDialog();
            DialogResult dr = MessageBox.Show("Do you want to save the file", "save", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.Equals(DialogResult.Yes)) //Statement that execute when user click on yes button
            {
                string filename = sfd.FileName;
                String filter = "Text Files|*.txt|All Files|*.*";
                sfd.Filter = filter;

                sfd.Title = "Save";

                if (sfd.ShowDialog(this) == DialogResult.OK)
                {

                    //Write all of the text in txtBox to the specified file


                    System.IO.File.WriteAllText(filename, richTextBox1.Text);



                }

                else
                {

                    //Return

                    return;

                }

            }
            else
            {
                richTextBox1.Clear();
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            DialogResult dr = MessageBox.Show("Do you want to save the file", "save", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //Declare open as a new OpenFileDailog
            OpenFileDialog open = new OpenFileDialog();
            //Set the Filename of the OpenFileDailog to nothing
            open.FileName = "";
            //Declare filename as a String equal to the OpenFileDialog's FileName
            String filename = open.FileName;
            //Declare filter as a String equal to our wanted OpenFileDialog Filter
            String filter = "Text Files|*.txt|All Files|*.*";
            //Set the OpenFileDialog's Filter to filter
            open.Filter = filter;
            //Set the title of the OpenFileDialog to Open
            open.Title = "Open";
            //Show the OpenFileDialog
            if (open.ShowDialog(this) == DialogResult.OK)
            {

                sfd.Filter = filter;

                sfd.Title = "Save";

                if (sfd.ShowDialog(this) == DialogResult.OK)
                {

                    //Write all of the text in txtBox to the specified file


                    System.IO.File.WriteAllText(filename, richTextBox1.Text);



                }

                else
                {

                    //Return

                    return;

                }
            }
            else
            {
                //Return
                return;
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {    //Declare save as a new SaveFileDailog

            SaveFileDialog sw = new SaveFileDialog();

            //Declare filename as a String equal to the SaveFileDialog's FileName

            string filename = sw.FileName;

            //Declare filter as a String equal to our wanted SaveFileDialog Filter

            String filter = "Text Files|*.txt|All Files|*.*";

            //Set the SaveFileDialog's Filter to filter

            sw.Filter = filter;

            //Set the title of the SaveFileDialog to Save

            sw.Title = "Save";

            //Show the SaveFileDialog

            if (sw.ShowDialog(this) == DialogResult.OK)
            {

                //Write all of the text in txtBox to the specified file


                System.IO.File.WriteAllText(filename, richTextBox1.Text);



            }

            else
            {

                //Return

                return;

            }

        }





        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Declare save as a new SaveFileDailog

            SaveFileDialog sw = new SaveFileDialog();

            //Declare filename as a String equal to the SaveFileDialog's FileName

            string filename = sw.FileName;

            //Declare filter as a String equal to our wanted SaveFileDialog Filter

            String filter = "Text Files|*.txt|All Files|*.*";

            //Set the SaveFileDialog's Filter to filter

            sw.Filter = filter;

            //Set the title of the SaveFileDialog to Save

            sw.Title = "Save";

            //Show the SaveFileDialog

            if (sw.ShowDialog(this) == DialogResult.OK)
            {

                //Write all of the text in txtBox to the specified file



                System.IO.File.WriteAllText(filename, richTextBox1.Text);


            }

            else
            {

                //Return

                return;

            }
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            //statement that execute on click of exit button   
            //and chekcing whether textbox modified or not if modified          
            //then prompt user to save or not          
            if (richTextBox1.Modified == true)
            {
                DialogResult dr = MessageBox.Show("Do you want to save the file before exiting", "unsaved file", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    string filename = sfd.FileName;
                    String filter = "Text Files|*.txt|All Files|*.*";
                    sfd.Filter = filter;
                    sfd.Title = "Save";

                    if (sfd.ShowDialog(this) == DialogResult.OK)
                    {

                        //Write all of the text in txtBox to the specified file


                        System.IO.File.WriteAllText(filename, richTextBox1.Text);



                    }

                    else
                    {

                        //Return

                        return;

                    }
                }
                else
                {
                    richTextBox1.Modified = false;
                    Application.Exit();
                }
            }
        }

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

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

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

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
        }
        //Declare prntDoc as a new PrintDocument
        System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument();

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Declare print as a new PrintDialog
            PrintDialog print = new PrintDialog();
            //Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
            prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
            //Set prntDoc to the PrintDialog's Document
            print.Document = prntDoc;
            //Show the PrintDialog
            if (print.ShowDialog(this) == DialogResult.OK)
            {
                //Print the Page
                prntDoc.Print();
            }
        }
        private void prntDoc_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //Declare g as Graphics equal to the PrintPageEventArgs Graphics
            Graphics g = e.Graphics;
            //Draw the Text in txtBox to the Document
            g.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 0, 0);
        }

        private void prientPriewevToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Declare preview as a new PrintPreviewDialog
            PrintPreviewDialog preview = new PrintPreviewDialog();
            //Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
            prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
            //Set the PrintPreview's Document equal to prntDoc
            preview.Document = prntDoc;
            //Show the PrintPreview Dialog
            if (preview.ShowDialog(this) == DialogResult.OK)
            {
                //Generate the PrintPreview
                prntDoc.Print();
            }
        }
        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            richTextBox1.Undo();
        }

        private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //performing wordwrap operation         
            if (wordWrapToolStripMenuItem.Checked == false)
            {
                wordWrapToolStripMenuItem.Checked = true;
                richTextBox1.WordWrap = true;
            }
            else
            {
                wordWrapToolStripMenuItem.Checked = false;
                richTextBox1.WordWrap = false;
            }
        }
        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Redo();
            
        }

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            fontDialog.ShowColor = true;
            fontDialog.ShowEffects = true;
            if (fontDialog.ShowDialog(this) == DialogResult.OK)
            {
                richTextBox1.ForeColor = fontDialog.Color;
                richTextBox1.Font = fontDialog.Font;
            }

        }

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            findform f = new findform();
            f.ShowDialog();
            if (Text != "")
            {
                richTextBox1.Find(Text);
            }
            
        }


        private void statusbarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //statuslabel1.Text = "Cols " + richTextBox1.Text.Length;    
            if (statusbarToolStripMenuItem.Checked == false)
            {
                statusbarToolStripMenuItem.Checked = true;

            }
            else
            {

                statusbarToolStripMenuItem.Checked = false;

            }
        }


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

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

        }

        private void goToToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }
}
But I did not write find,find next and pagesetup
 
please help me   




Posted
Comments
syed shanu 29-Aug-14 4:57am    
Check this links :
http://www.codeproject.com/Articles/7313/Notepad-Application-in-C

http://www.c-sharpcorner.com/uploadfile/rajshree.mittal/notepad-in-c-sharp/

http://www.sourcecodester.com/tutorials/c/6562/simple-notepad-application-using-c-part-2.html

 
Share this answer
 
Comments
goksurahsan 29-Aug-14 6:22am    
I search these and I did a lot but I didn't write page setup :(
 
Share this answer
 
Comments
goksurahsan 29-Aug-14 6:15am    
thank you for helping
Gihan Liyanage 29-Aug-14 6:21am    
You are welcome..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900