C# Creating your own Notepad Application Tutorial

In this tutorial I will show you how you can create your own Notepad like Application.

Start by creating a new Windows Forms Application.

Now we can add some controls. The first control will be a MenuStrip.

After that right click on the newly created menustrip, Select Insert Standard Items. Delete the Tools menu and the Help menu. Delete the Redo menu item from the Edit menu.

Now drag and drop a TextBox onto the form.


 Make sure that the textbox can have more than one line, by selecting the multiline checkbox.


Type in txtBox for the name of our newly created textbox. Set the Dock to Fill

Now we will do some coding.

For the New menu item's Click() event type in txtBox.Clear();
For the Open menu item's Click() event type in

//Declare open as a new OpenFileDailog
OpenFileDialog open = new OpenFileDialog();
//Set the Filename of the OpenFileDailog to nothing
open.FileName = "";
//Declare filename as a String equal to the OpenFileDialog's FileName
String filename = open.FileName;
//Declare filter as a String equal to our wanted OpenFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the OpenFileDialog's Filter to filter
open.Filter = filter;
//Set the title of the OpenFileDialog to Open
open.Title = "Open";
//Show the OpenFileDialog
if (open.ShowDialog(this) == DialogResult.OK)
{
 //Make the txtBox's Text equal to all of the text in the OpenFileDialog's FileName(filename)
 txtBox.Text = System.IO.File.ReadAllText(filename);
}
else
{
 //Return
 return;
} 
 
For the Save and Save As menu items' Click() event type in
 
//Declare save as a new SaveFileDailog
SaveFileDialog save = new SaveFileDialog();
//Declare filename as a String equal to the SaveFileDialog's FileName
String filename = save.FileName;
//Declare filter as a String equal to our wanted SaveFileDialog Filter
String filter = "Text Files|*.txt|All Files|*.*";
//Set the SaveFileDialog's Filter to filter
save.Filter = filter;
//Set the title of the SaveFileDialog to Save
save.Title = "Save";
//Show the SaveFileDialog
if (save.ShowDialog(this) == DialogResult.OK)
{
 //Write all of the text in txtBox to the specified file
 System.IO.File.WriteAllText(filename, txtBox.Text);
}
else
{
 //Return
 return;
} 
 
After enter this code
 
//Declare prntDoc as a new PrintDocument
System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument(); 
 
For the Print menu items' Click() event type in
 
//Declare print as a new PrintDialog
PrintDialog print = new PrintDialog();
//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
//Set prntDoc to the PrintDialog's Document
print.Document = prntDoc;
//Show the PrintDialog
if (print.ShowDialog(this) == DialogResult.OK)
{
 //Print the Page
 prntDoc.Print();
} 
 
Now enter this code right after the print item's Click() event
 
private void prntDoc_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
 //Declare g as Graphics equal to the PrintPageEventArgs Graphics
 Graphics g = e.Graphics;
 //Draw the Text in txtBox to the Document
 g.DrawString(txtBox.Text, txtBox.Font, Brushes.Black, 0, 0);
} 
 
Now enter this code for the Print Preview menu item's Click() event
 
//Declare preview as a new PrintPreviewDialog
PrintPreviewDialog preview = new PrintPreviewDialog();
//Declare prntDoc_PrintPage as a new EventHandler for prntDoc's Print Page
prntDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(prntDoc_PrintPage);
//Set the PrintPreview's Document equal to prntDoc
preview.Document = prntDoc;
//Show the PrintPreview Dialog
if (preview.ShowDialog(this) == DialogResult.OK)
{
 //Generate the PrintPreview
 prntDoc.Print();
} 
Now go to the Exit menu item's Click() event and type Close;

Now go to the Undo menu item's Click() event and type txtBox.Undo();

Now go to the Cut menu item's Click() event and type txtBox.Cut();
Now go to the Copy menu item's Click() event and type txtBox.Copy();

Now go to the Paste menu item's Click() event and type txtBox.Paste();

Now go to the Select All menu item's Click() event and type txtBox.SelectAll();



Finished! Now You Have Done :D
 


Previous Post Next Post