TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Create Simple Notepad Application using C# in Visual Studio 2012
Guest User
Nov 24
2014
Code
22.4
k
0
3
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
notpad.rar
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
using
System.IO;
namespace
notpad
{
public
partial
class
Notepad : Form
{
string
pt =
""
;
bool
save_flag =
false
;
int
countss = 0;
public
Notepad()
{
InitializeComponent();
}
private
void
fontToolStripMenuItem_Click(
object
sender, EventArgs e)
{
if
(fontDialog1.ShowDialog() == DialogResult.OK)
{
if
(richTextBox1.SelectedText !=
""
)
{
richTextBox1.Font = fontDialog1.Font;
}
}
}
private
void
openToolStripMenuItem_Click(
object
sender, EventArgs e)
{
OpenFileDialog openFileDialog =
new
OpenFileDialog();
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter =
"Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
;
if
(openFileDialog.ShowDialog(
this
) == DialogResult.OK)
{
string
path = openFileDialog.FileName;
pt = path;
int
pos = path.LastIndexOf(
"\\"
);
string
ss = path.Substring(pos + 1);
this
.Text =
"Notepad - "
+ ss;
// Open the file to read from.
using
(StreamReader sr = File.OpenText(path))
{
string
s =
""
;
while
((s = sr.ReadLine()) !=
null
)
{
this
.richTextBox1.AppendText(s + Environment.NewLine);
countss = countss + s.Length + 1;
}
}
}
}
private
void
newToolStripMenuItem_Click(
object
sender, EventArgs e)
{
if
(
this
.Text ==
"Notepad - Untitled"
)
{
string
s = from_close();
this
.richTextBox1.Clear();
}
//notpad.Notepad n = new Notepad();
//n.Show();
}
private
void
printToolStripMenuItem_Click(
object
sender, EventArgs e)
{
printDialog1.ShowDialog();
}
private
void
saveToolStripMenuItem_Click(
object
sender, EventArgs e)
{
string
path =
""
;
saveFileDialog1.DefaultExt =
".txt"
;
saveFileDialog1.FileName =
"*.txt"
;
saveFileDialog1.Filter =
"Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
;
if
(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
path = saveFileDialog1.FileName;
pt = path;
if
(!File.Exists(path))
//if file does not exits
{
// Create a file to write to.
using
(StreamWriter sw = File.CreateText(path))
{
for
(
int
i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
countss = countss + richTextBox1.Lines[i].Length + 1;
save_flag =
true
;
}
}
countss = countss - 1;
}
else
if
(File.Exists(path))
//if file is exist
{
using
(StreamWriter sw = File.CreateText(path))
{
for
(
int
i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
countss = countss + richTextBox1.Lines[i].Length + 1;
save_flag =
true
;
}
}
}
}
int
pos = path.LastIndexOf(
"\\"
);
string
ss = path.Substring(pos + 1);
this
.Text =
"Notepad - "
+ ss;
}
private
void
undoToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.Undo();
}
private
void
cutToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.Cut();
change_Pest();
}
private
void
copyToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.Copy();
change_Pest();
}
private
void
pestToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.Paste();
}
private
void
deleteToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.Cut();
change_Pest();
}
private
void
findToolStripMenuItem_Click(
object
sender, EventArgs e)
{
}
private
void
replaceToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.FindForm();
}
private
void
printSetupToolStripMenuItem_Click(
object
sender, EventArgs e)
{
}
private
void
exitToolStripMenuItem_Click(
object
sender, EventArgs e)
{
// from_close();
this
.Close();
}
private
void
editToolStripMenuItem_Click(
object
sender, EventArgs e)
{
}
private
void
richTextBox1_TextChanged(
object
sender, EventArgs e)
{
findToolStripMenuItem.Enabled =
true
;
findnextToolStripMenuItem.Enabled =
true
;
replaceToolStripMenuItem.Enabled =
true
;
undoToolStripMenuItem.Enabled =
true
;
cutToolStripMenuItem.Enabled =
false
;
copyToolStripMenuItem.Enabled =
false
;
deleteToolStripMenuItem.Enabled =
false
;
//MessageBox.Show("text selected");
}
private
void
richTextBox1_MouseCaptureChanged(
object
sender, EventArgs e)
{
cutToolStripMenuItem.Enabled =
true
;
copyToolStripMenuItem.Enabled =
true
;
deleteToolStripMenuItem.Enabled =
true
;
}
private
void
change_Pest()
{
cutToolStripMenuItem.Enabled =
false
;
copyToolStripMenuItem.Enabled =
false
;
deleteToolStripMenuItem.Enabled =
false
;
pestToolStripMenuItem.Enabled =
true
;
}
private
void
Notepad_FormClosing(
object
sender, FormClosingEventArgs e)
{
from_close();
}
private
void
dateTimeToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.AppendText(DateTime.Now.ToShortDateString().ToString() +
" "
+ DateTime.Now.ToShortTimeString().ToString());
}
private
void
menuStrip1_ItemClicked(
object
sender, ToolStripItemClickedEventArgs e)
{
}
private
void
Notepad_Load(
object
sender, EventArgs e)
{
this
.Text =
"Notepad -"
+
" Untitled"
;
this
.richTextBox1.Clear();
}
private
void
viewToolStripMenuItem_Click(
object
sender, EventArgs e)
{
}
private
string
from_close()
{
string
rtn_string =
""
;
if
(richTextBox1.Modified ==
true
)
{
if
(
this
.Text ==
"Notepad - Untitled"
)
{
DialogResult dr = MessageBox.Show(
"Do you want to save changes to Untitled ?"
,
"Notepad"
, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if
(dr == DialogResult.Yes)
{
saveFileDialog1.DefaultExt =
".txt"
;
saveFileDialog1.FileName =
"*.txt"
;
saveFileDialog1.Filter =
"Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
;
if
(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string
path = rtn_string = saveFileDialog1.FileName;
if
(!File.Exists(path))
//if file does not exits
{
// Create a file to write to.
using
(StreamWriter sw = File.CreateText(path))
{
for
(
int
i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
}
}
}
else
if
(File.Exists(path))
//if file is exist
{
using
(StreamWriter sw = File.CreateText(path))
{
for
(
int
i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
}
}
}
}
}
}
else
//if text file is existing
{
// MessageBox.Show(richTextBox1.Text.Length+" "+countss);
if
(richTextBox1.Text.Length != countss || save_flag ==
false
)
{
MessageBox.Show(
"Do you want to save changes to "
+ pt +
" ?"
,
"Notpad"
, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
using
(StreamWriter sw = File.CreateText(pt))
{
for
(
int
i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
}
}
}
}
}
return
rtn_string;
}
private
void
aboutNotepadToolStripMenuItem_Click(
object
sender, EventArgs e)
{
Form2 f =
new
Form2();
f.ShowDialog();
}
private
void
saveAsToolStripMenuItem_Click(
object
sender, EventArgs e)
{
saveFileDialog1.DefaultExt =
".txt"
;
saveFileDialog1.FileName =
"*.txt"
;
saveFileDialog1.Filter =
"Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
;
if
(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string
path = saveFileDialog1.FileName;
if
(!File.Exists(path))
//if file does not exits
{
// Create a file to write to.
using
(StreamWriter sw = File.CreateText(path))
{
for
(
int
i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
}
}
}
else
if
(File.Exists(path))
//if file is exist
{
using
(StreamWriter sw = File.CreateText(path))
{
for
(
int
i = 0; i < richTextBox1.Lines.Length; i++)
{
sw.WriteLine(richTextBox1.Lines[i]);
}
}
}
}
}
private
void
wordWrapToolStripMenuItem_Click(
object
sender, EventArgs e)
{
}
private
void
viewHelpToolStripMenuItem_Click(
object
sender, EventArgs e)
{
Form2 f =
new
Form2();
f.ShowDialog();
}
private
void
selectAllToolStripMenuItem_Click(
object
sender, EventArgs e)
{
richTextBox1.SelectAll();
}
}
}
C#
Visual Studio