Making Notepad Application With New Features Using
C# :
Notepad is a familiar and famous
and basic text editor program that exists on Windows computer with very basic
text editing capability.
We can make our own notepad
application for Windows operating system using C# programming. The programming
techniques, code and screenshots used in this tutorial are in compliance with
the latest versions of the following tools and technologies.
C#(Code)..............
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 notepad
{
public
partial class Form1 : Form
{
PrinterSettings prs;
PageSettings pgs;
string dmyfilename = "";
DialogResult dr = DialogResult.No;
string filetype = "";
public Form1()
{
InitializeComponent();
}
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
private void redoToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Redo();
}
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();
}
private void
deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void statusBarToolStripMenuItem_Click(object sender, EventArgs
e)
{
if (statusBarToolStripMenuItem.Checked == true)
{
statusStrip1.Visible = false;
statusBarToolStripMenuItem.Checked = false;
}
else
{
statusStrip1.Visible=true;
statusBarToolStripMenuItem.Checked=true;
}
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
richTextBox1.SelectionFont = fontDialog1.Font;
}
private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
{
if (wordWrapToolStripMenuItem.Checked == true)
{
richTextBox1.WordWrap = false;
wordWrapToolStripMenuItem.Checked
= false;
}
else
{
richTextBox1.WordWrap = true;
wordWrapToolStripMenuItem.Checked = true;
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Length > 0)
{
dr = MessageBox.Show(null, "do
u want to savefile", "mynotepad", MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
if (dr == DialogResult.Yes)
{
saveToolStripMenuItem_Click(sender, e);
richTextBox1.Text = "";
}
else if (dr == DialogResult.Yes)
{
richTextBox1.Text = "";
this.Text = "untitled_mynotepad";
dmyfilename = " ";
}
else if (dr == DialogResult.No)
{
richTextBox1.Text = "";
this.Text = "untitled_mynotepad";
dmyfilename = " ";
}
}
}
private
void openToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
openFileDialog1.Filter = "TextDocument|*.txt|MyNotepadFormat|*.sup|Encrypted
Format|*.supx|AllFiles|*.*";
openFileDialog1.ShowDialog();
dmyfilename =
openFileDialog1.FileName;
filetype =
openFileDialog1.FilterIndex.ToString();
StreamReader sr = new StreamReader(openFileDialog1.FileName);
if (filetype == "2")
richTextBox1.Rtf =
sr.ReadToEnd();
else if (filetype == "1"
|| filetype == "4")
richTextBox1.Text =
sr.ReadToEnd();
else if (filetype == "3")
richTextBox1.Rtf= crypt.Decrypt(sr.ReadToEnd(),true
);
sr.Close();
int po =
dmyfilename.LastIndexOf("\\");
dmyfilename = dmyfilename =
dmyfilename.Substring(po + 1);
// po =
dmyfilename.LastIndexOf(".");
// dmyfilename =
dmyfilename.Substring(0, po);
this.Text = dmyfilename + "
- MyNotepad";
}
catch (Exception Ex)
{
MessageBox.Show("Invalid
File Format");
}
}
void
savefile(string filename)
{
StreamWriter sw = new StreamWriter(filename);
if (filetype == "2")
{
sw.Write(richTextBox1.Rtf);
}
else if (filetype == "1" || filetype == "4")
sw.Write(richTextBox1.Text);
else if (filetype == "3")
sw.Write(crypt.Encrypt(richTextBox1.Rtf, true));
sw.Close();
}
void
savedialogshow()
{
saveFileDialog1.Filter = "TextDocument|*.txt|MyNotepadFormat|*.sup|Encrypted
Format|*.supx|AllFiles|*.*";
saveFileDialog1.ShowDialog();
filetype = saveFileDialog1.FilterIndex.ToString();
dmyfilename = saveFileDialog1.FileName;
if (dmyfilename != "")
{
// MessageBox.Show(saveFileDialog1.FilterIndex.ToString());
savefile(dmyfilename);
int po = dmyfilename.LastIndexOf("\\");
dmyfilename = dmyfilename.Substring(po + 1);
//po =
dmyfilename.LastIndexOf(".");
//dmyfilename = dmyfilename.Substring(0, po);
this.Text = dmyfilename + " - MyNotepad";
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dmyfilename == "")
{
savedialogshow();
}
else
{
savefile(saveFileDialog1.FileName);
MessageBox.Show(saveFileDialog1.FileName);
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
savedialogshow();
}
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
richTextBox1.SelectionColor = colorDialog1.Color;
}
private
void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
newToolStripMenuItem_Click(sender, e);
if (dr != DialogResult.Cancel)
{
Application.Exit();
}
}
private
void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
prs = new PrinterSettings();
pgs = new PageSettings();
pgs.PrinterSettings = prs;
pageSetupDialog1.PageSettings = pgs;
pageSetupDialog1.ShowDialog();
}
private
void printToolStripMenuItem_Click(object sender, EventArgs e)
{
prs = new PrinterSettings();
printDialog1.PrinterSettings = prs;
printDialog1.ShowDialog();
printDialog1.Document = printDocument1;
printDocument1.Print();
}
private
void timedateToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = DateTime.Now.ToString();
}
private
void richTextBox1_TextChanged(object sender, EventArgs e)
{
//toolStripStatusLabel1.Text = "done";
}
private
void toolStripStatusLabel1_TextChanged(object sender, EventArgs e)
{
}
private
void toolStripTextBox1_TextChanged(object sender, EventArgs e)
{
int po = richTextBox1.Text.IndexOf(txtfind.Text);
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.Text.Length;
richTextBox1.SelectionColor = Color.Black;
if (po >= 0)
{
richTextBox1.SelectionStart = po;
richTextBox1.SelectionLength = txtfind.Text.Length;
richTextBox1.SelectionColor = Color.Red ;
}
}
private
void findToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private
void gotoToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private
void editToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private
void formatToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private
void findNextToolStripMenuItem_Click(object sender, EventArgs e)
{
// startindex = Po + toolStripTextBox1.Text.Length;
// Textfind_Textchanged(sender,e);
}
private
void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
{
newToolStripMenuItem_Click(sender, e);
if (dr != DialogResult.Cancel)
Application.Exit();
}
private
void findToolStripMenuItem_TextChanged(object sender, EventArgs e)
{
//int po=richTextBox1.Text.IndexOf(find
}
private
void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
}
private
void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
int lineno =
richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
int col = richTextBox1.SelectionStart -
richTextBox1.GetFirstCharIndexFromLine(lineno) + 1;
toolStripStatusLabel1.Text = "Ln," + lineno.ToString() + " Col," + col.ToString();
}
private
void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//richTextBox1.SelectionStart =
richTextBox1.GetFirstCharIndexFromLine(int.Parse(txtgoto.Text) - 1);
//richTextBox1.Focus();
int lineno =
richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
int col =richTextBox1.SelectionStart-
richTextBox1.GetFirstCharIndexFromLine(lineno)+1;
//col++;
//int col = richTextBox1.SelectionStart;// richTextBox1.GetCharIndexFromPosition(richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart));
toolStripStatusLabel1.Text = "Ln," +(lineno+1).ToString() + " Col," + col.ToString() ;
}
private
void aboutNotepadToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 aboutbox = new AboutBox1();
aboutbox.ShowDialog();
}
private
void helpTopicsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private
void toolStripButton2_Click(object sender, EventArgs e)
{
}
private
void replaceToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private
void toolStripButton11_Click(object sender, EventArgs e)
{
//richTextBox1.SelectionLength =
richTextBox1.Lines[int.Parse(txtgoto.Text)-1].Length;
}
private
void toolStripButton5_Click(object sender, EventArgs e)
{
}
private
void toolStripButton12_Click(object sender, EventArgs e)
{
if (txtfind.Visible == true)
txtfind.Visible = false;
else
{
txtfind.Visible = true;
txtfind.Focus();
}
}
private
void toolStripLabel1_Click(object sender, EventArgs e)
{
if (txtgoto.Visible == true)
txtgoto.Visible = false;
else
{
txtgoto.Visible = true;
txtgoto.Focus();
}
}
private
void txtgoto_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar==(char)Keys.Enter)
{
richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(int.Parse(txtgoto.Text)
- 1);
richTextBox1.Focus();
}
}
private
void txtreplace_KeyPress(object sender, KeyPressEventArgs e)
{
int key = (int)e.KeyChar;
if (key == 13)
{
int po = richTextBox1.Text.IndexOf(txtfind.Text);
richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = richTextBox1.Text.Length;
richTextBox1.SelectionColor = Color.Black;
if (po >= 0)
{
richTextBox1.SelectionStart =
po;
richTextBox1.SelectionLength
= txtfind.Text.Length;
richTextBox1.SelectedText =
txtreplace.Text;
}
}
}
private
void toolStripLabel2_Click(object sender, EventArgs e)
{
if (txtreplace.Visible == true)
txtreplace.Visible = false;
else
{
txtreplace.Visible = true;
txtreplace.Focus();
}
}
private
void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private
void toolStripButton9_Click(object sender, EventArgs e)
{
calendar objcal = new calendar();
objcal.ShowDialog();
}
private
void toolStripButton10_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("c:\\windows\\system32\\calc.exe");
}
catch (Exception Ex)
{
MessageBox.Show("Calculator is not Accessable");
}
}
private
void bulletsNumberToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionBullet == false)
richTextBox1.SelectionBullet = true;
else
richTextBox1.SelectionBullet = false;
}
private
void Form1_Load(object sender, EventArgs e)
{
}
private
void co(object sender, EventArgs e)
{
}
private
void xx(object sender, EventArgs e)
{
}
private
void txtgoto_Click(object sender, EventArgs e)
{
}
private
void ppToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
//printDialog1.ShowDialog();
//printDocument1.Print();
}
private
void button1_Click(object sender, EventArgs e)
{
}
private
void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black,
100, 100);
e.Graphics.PageUnit = GraphicsUnit.Inch;
}
}
}
Nice
ReplyDelete-samdani
tqs
Delete