In this article we will see how we can create a simple app like notepad in Windows Application
Introduction
This is a Windows Application which resembles notepad. We can implement functionality such
File Menu,Edit Menu,Format Menu.
- The File menu has menu options New,Open,Save,Print Preview,Print,Exit.
- The Edit menu has Cut,Copy,Paste and Undo
- The Format Menu has Font and Page Setup
Objective
To Create a Application such as Notepad.
Using the code
Open MS VISUAL STUDIO 2010 -> File -> New Project -> Windows Application -> Right Click on the Solution and Add a Windows Form.
Go to the properties of Windows form and set its IsMDiContainer Property to True.
Now go to the Toolbox and Drag and Drop a Rich TextBox Control on the Form.
Now go to the Tool box and drag and Drop a MenuStrip on the top portion of the form.
Now click on the Menu Strip and Type File on the First Position.This will be called menu strip items.
Now Under the File Menu Item add the following items one below another as shown in the figure.
File Menu
Now just beside the File Menu Item add another Menu Item on the menu Tool Strip as EDIT as seen in the above Image.
Once you have added the MenuStrip Item now start adding Menu Items as we did it for File Menu.Below is the Image for Edit Menu.
Edit Menu
Now just beside the Edit Item add another Menu Item on the menu Tool Strip as Format as seen in the above Image.
Once you have added the MenuStrip Item now start adding Menu Items as we did it for Edit Menu.Below is the Image for Format Menu as shown in the below image
Format Menu
Now from the Tool box just drag the following controls from the Tool Box.
- Save Dialog Box :- This will help to save the text written in RichTextBox as .txt file.
- Open File Dialog Box :- This will help to open the the Notepad files on our computer and read the content in the rich text box of our form
- Font Dialog Box :- This will help us to change the font of the text in the Rich Text box.
- Page Set up Dialog box :- This will help us to set the page up for printing.
- printDocument :- This will help us to print our text that we have written it in Rich text box
- Print Dialog 1 :- This will call the Print Confirmation box and help us to choose the printer that is connected to the system
- PrintPreviewDialogBox :- To Preview what we are going to print.
Check the Snapshot to view where these tools will be placed
Namespaces to add
using System.IO;
using System.Drawing.Printing;
These namespaces are to be added to use the Print and Print View Functionalities and reading a Text file.
// Code for the Entire App
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// This will close the application if you click on exit in the File Menu
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to Exit", "Quit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
// This will clear the rich text box.
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
}
//This will allow to save the text that we have typed in the Rich Text Box in a .txt file
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "unknown.txt";
saveFileDialog1.Filter = "text file(*.txt)|*.txt|All files(*.*)|*.*"; // to save the file as notepad.
if (saveFileDialog1.ShowDialog()== DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
{
sw.WriteLine(richTextBox1.Text);
}
}
}
// This will open the Open dialog box and allow us to select the notepad file and read the content in the Rich Text box.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}
}
// This will allow us to copy the text written in the text box.This will only work after you select the text in the Rich text box
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0) richTextBox1.Copy();
}
// This will allow us to paste the text written in the text box.This will only work after you select the text in the Rich text box
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
// This will allow us to cut the text written in the text box.This will only work after you select the text in the Rich text box
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText != "") richTextBox1.Cut();
}
// This will allow us to undo the changes in the text box.
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.CanUndo == true) richTextBox1.Undo();
}
// This will allow us to open the font dialog box and change the font in the for the text in the Rich Text box.The font will only change when you select the text
// and select the font from the font dialog box.
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText != "")
{
richTextBox1.Select();
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
}
}
}
// This will open page setup dialog box and allow you to set up the page settings
private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
{
PageSetupDialog setupDlg = new PageSetupDialog();
setupDlg.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
setupDlg.PageSettings = new System.Drawing.Printing.PageSettings();
setupDlg.EnableMetric = false;
setupDlg.ShowDialog();
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
}
// This will call the print dialog box to select the printer.
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument.Print();
}
}
// This will allow us to see print preview before printing.
private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument;
printPreviewDialog1.ShowDialog();
}
// This will send the Rich Text Box data to print.
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
e.Graphics.PageUnit = GraphicsUnit.Inch;
}
}
}
Conclusion
I hope this will help the dot net programmers......
Sharing is Caring.....