Creating sample Notepad Application using C#
Here we are creating Notepad Application using C# with following functionality.
File: New,Open,Save,Print,Exit
Edit:Undo,Cut,Copy,Paste,Delete,Find,Select All,Date/Time
Format:Font,Color
Help:About Notepad X
First Open New Window Form Application in Visual studio.
In design add MenuStrip on the top and add menu items.You can also define shortcut for menu items by right clicking on item >Go to properties >ShortCutKeys
Add RichTextBox to the form. In our example we named it as txtcontent.
Now add the following dialog from Toolbox.
saveFileDialog
openFileDialog
fontDialog
colorDialog
printDialog
Code :
For font and color
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
txtcontent.Font = fontDialog1.Font;
}
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
txtcontent.ForeColor = colorDialog1.Color;
}
-----------------------------------------------------------------
For Undo Cut,Copy,Paste,Delete,Select All and Date/Time
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
txtcontent.Undo();
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
txtcontent.Cut();
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
txtcontent.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
txtcontent.Paste();
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
txtcontent.SelectedText="";
}
private void selectAlllToolStripMenuItem_Click(object sender, EventArgs e)
{
txtcontent.SelectAll();
}
private void dateTimeToolStripMenuItem_Click(object sender, EventArgs e)
{
txtcontent.AppendText(DateTime.Now.ToString());
}
-----------------------------------------------------------------
New
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
txtcontent.Text = "";
}
Open
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
//Now it show open dialog box.
//Read the filename selected by the user with in open file dialog box.
string fName = openFileDialog1.FileName;
//Read the data by using StreamReader class
if (fName != "")
{
StreamReader sr = new StreamReader(fName);
txtcontent.Text = sr.ReadToEnd();
sr.Close();
}
}
Save
private void saveCtrlSToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.ShowDialog();
string fName = saveFileDialog1.FileName;
if (fName != "")
{
StreamWriter sw = new StreamWriter(fName);
sw.Write(txtcontent.Text);
sw.Flush();
sw.Close();
}
}
For Printing
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.ShowDialog();
}
Exit
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
-----------------------------------------------------------------
About Notepad X
Add > New Item > About Box
Now go to properties and open AssemblyInfo.cs
Edit Information here as shown below.
In Main form (here Form1.cs) add following code for clicking action.
private void aboutNotepadXToolStripMenuItem_Click(object sender, EventArgs e)
{
About abt = new About();
abt.Show();
}
------------------------------------------------------------------------------------------------------------
For find functionality, Click Here
http://futurextech.blogspot.com/2013/12/implementing-find-function-in-sample.html
------------------------------------------------------------------------------------------------------------
http://futurextech.blogspot.com/2013/12/implementing-find-function-in-sample.html
------------------------------------------------------------------------------------------------------------
0 comments: