A Simple Notepad In C#



This guide wont teach you to code some awesome notepad but just a simple one..Im going to try to keep the code as simple as possible.


Aite, your gonna need to open a new C# project by opening Microsoft Visual C#, then goto 



File>New>Windows Form Application

Name the project Notepad or whatever you want.


Add the following items. (Menu Strip, openFileDialog, saveFileDialog, Text Box)



Posted Image


Now set the textBox's dock to fill, then rename it to txtMain.


Posted Image


Then click one time on your textBox, you should see a small play arrow, click it and check "Multiline". 


Posted Image


You should now have something that looks like this.


Posted Image


Now right click on a blank part of the menu strip, and press "Insert Standard Items"


Posted Image


You should have a bunch of basic functions by now, but its not that easy =P, they all dont have code. 


But start off by deleting "Tools" and "Help". 


Now in your form editor click on the "File" button, then double click on "New", which should bring you to the code editor, then write the code



txtMain.Clear();

Then goto "Open"'s code, and add this code



//Shows the openFileDialog
openFileDialog1.ShowDialog();
//Reads the text file
System.IO.StreamReader OpenFile = new System.IO.StreamReader(openFileDialog1.FileName);
//Displays the text file in the textBox
txtMain.Text = OpenFile.ReadToEnd();
//Closes the proccess
OpenFile.Close();

Now proceed to "Save"'s code, and add this code



//Determines the text file to save to
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(openFileDialog1.FileName);
//Writes the text to the file
SaveFile.WriteLine(txtMain.Text);
//Closes the proccess
SaveFile.Close();

Then for "Save as"'s code, add



//Open the saveFileDialog
saveFileDialog1.ShowDialog();
//Determines the text file to save to
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
//Writes the text to the file
SaveFile.WriteLine(txtMain.Text);
//Closes the proccess
SaveFile.Close();

"Print"'s code is



//Declare prntDoc as a new PrintDocument
System.Drawing.Printing.PrintDocument prntDoc = new System.Drawing.Printing.PrintDocument();

"Print Preview" is



//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();
}

"Exit"'s code is



Application.Exit();

Move on to "Undo"'s code, add



txtMain.Undo();

For "Redo"'s code its



txtMain.Undo();

Add this to "Cut"'s code



txtMain.Cut();

For "Copy" add



txtMain.Copy();

"Paste"'s code is



txtMain.Paste();

For "Select All" its



txtMain.SelectAll();

Ur done with most of the code, but our Notepad still lacks one function - Word Wrap!!


Quote from Xav:



Xav said:
I disagree with the last bit (the word wrap). It would be much better to use a single checked box to wrap text.

To achieve this, set the menu item's Checkonclick boolean property to True, and then use the following code in the Click event:



txtMain.WordWrap = wordWrapMenuStripItem.Checked



Well thats it =)


Posted Image


Hope you enjoyed this guide. If you need anymore screenies tell me lol, i'll add em

No comments:

Post a Comment