Ad

Sunday, July 14, 2013

Design A Note Pad Application Using Windows Forms



Overview:
                In this article I would like to Demonstrate about a NotePad Application.
Description:
                This is application can be developed using the concept of

  1. MenuStrip
  2. Dialog Boxes
  3. Dialog Result
For this application first take following tools:
menustrip1,savefiledialog1,openfiledialog1,statustrip1

Dont Confuse with events and methods itself.......events succedded Example:cutToolStripMenuItem_Click(object sender, EventArgs e)
Program:

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;

namespace Notepad
{
    public partial class Form1 : Form
    {
        public SaveFileDialog sfd;
        public OpenFileDialog ofd;
        public int index;
        string fname = null;
        //bool modified = false;
      
        public Form1()
        {
            sfd = new SaveFileDialog();
            ofd = new OpenFileDialog();
        
            InitializeComponent();
        }

      

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

        private void timeDateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text += Convert.ToString(DateTime.Now);   
        }

        private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void openfile()
        {
            openFileDialog1.ShowDialog();
            fname = openFileDialog1.FileName;
            richTextBox1.SaveFile(fname, RichTextBoxStreamType.PlainText);
            this.Text = fname;
            modified = false;
        }
        private void savefile()
        {
            DialogResult d = saveFileDialog1.ShowDialog();
            if (d != DialogResult.Cancel)
            {
                fname = saveFileDialog1.FileName;
                richTextBox1.SaveFile(fname, RichTextBoxStreamType.PlainText);
            }
        }


        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text.Trim().Length > 0)
            {
                DialogResult dr = MessageBox.Show("the text in the untitled file has changed.\\n do u want to save the changes?", "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (dr.Equals(DialogResult.Yes))
                {
                    savefile();
                    richTextBox1.Clear();

                }
                else if (dr.Equals(DialogResult.No))
                {
                    richTextBox1.Clear();
                }

            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            savefile();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Modified == true)//checking either richtext box have entered value or not    
            {
                DialogResult dr = MessageBox.Show("Do you want to save changes to the opened file", "unsaved document", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.No)
                {

                    richTextBox1.Modified = false;
                    openfile();
                }
                else
                {
                    if (this.Text == "Untitled-Digital Diary")    
                    {
                        savefile();
                        openfile();
                    }
                    else
                    {
                        DialogResult dr1 = MessageBox.Show("the text in the file has been changed.Do you want to save the changes", "Open", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr1 == DialogResult.Yes)
                        {
                            richTextBox1.SaveFile(this.Text);
                            openfile();
                        }
                        else
                        {
                            openfile();
                        }
                    }
                }
            }
            else
            {
                openfile();
            }

        }

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

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            sfd.Title = "Save As";
            sfd.Filter = "Text Document|*.txt";
            sfd.DefaultExt = "txt";
            sfd.ShowDialog();

            this.Text = sfd.FileName;


        }

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

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

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

        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip1.Text = "used to redo last changed text";
            if (richTextBox1.CanRedo)
            {
                richTextBox1.Redo();
            }

        }

        private void statusStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

       

        private void fontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            fd.Font = richTextBox1.SelectionFont;
            fd.Color = richTextBox1.SelectionColor;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.SelectionFont = fd.Font;
                richTextBox1.SelectionColor = fd.Color;
            }

        }

        private void wordwrapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (wordwrapToolStripMenuItem.Checked == false)
            {
                wordwrapToolStripMenuItem.Checked= true;
                richTextBox1.WordWrap = true;
            }
            else
            {
                wordwrapToolStripMenuItem.Checked = false;
                richTextBox1.WordWrap = false;
            }


        }

        private void bGcolorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cr = new ColorDialog();
            if (cr.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.BackColor = cr.Color;
            }


        }
    }
}

OutPut:
NotePad



No comments: