This is a Two-Part Tutorial on Creating and Extending the Notepadd App.
Create a Notepad AppIn 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
CODE
//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
CODE
//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
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
CODE
//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
CODE
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
CODE
//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!
Don't forget I have included the source files:
Notes.zip ( 63.59k )
Number of downloads: 89Extend the Notepad AppIn this tutorial I will show you how to extend your notepad app we created in my tutorial. If you have not read my tutorial then click
here.
We will add some Menu items to the MenuStrip. The first Item will we a new Menu Drop Down called Format.
In the format Drop Down Menu add a menu item called Word Wrap. Set the Checked Property to
true, set the CheckOnClick Property to
true. Now select the TextBox(txtBox) and set the WordWrap property to
true.
Now go back to the Format menu and add one more menu item call it Font.
Drag a FontDialog onto the Form, call it fontDialog

Go to the Font menu item's Click() event and type
CODE
fontDialog.ShowColor = true;
fontDialog.ShowEffects = true;
if (fontDialog.ShowDialog(this) == DialogResult.OK)
{
txtBox.ForeColor = fontDialog.Color;
txtBox.Font = fontDialog.Font;
}
Now we will add some shortcut keys.
For the New menu item's shortcut key type in
Ctrl+NFor the Open menu item's shortcut key type in
Ctrl+OFor the Save menu item's shortcut key type in
Ctrl+SFor the Save As menu item's shortcut key type in
Ctrl+Shift+SFor the Print menu item's shortcut key type in
Ctrl+PFor the Print Preview menu item's shortcut key type in
Ctrl+Shift+PFor the Undo menu item's shortcut key type in
Ctrl+ZFor the Cut menu item's shortcut key type in
Ctrl+XFor the Copy menu item's shortcut key type in
Ctrl+CFor the Paste menu item's shortcut key type in
Ctrl+VFor the Select All menu item's shortcut key type in
Ctrl+AFor the Font menu item's shortcut key type in
Alt+FNow we will add a StatusStrip()

Make sure that it is docked to the bottom, then Right Click on txtBox and select
Bring to Front.
Now add a StatusLabel to the StatusStrip by selecting the small arrow on the StatusStrip, call it statusLabel.
Now go to the txtBox's TextChanged() event and type
CODE
Int32 lines = txtBox.Lines.Length;
Int32 textLength = txtBox.Text.Length;
statusLabel.Text = "Lines: " + lines + " Characters: " + textLength;
Now paste that in the Form's Load() event as well.
Now go to the Word Wrap menu item's Click() event and type
CODE
txtBox.WordWrap = wordWrapToolStripMenuItem.Checked;
wordWrapToolStripMenuItem.Checked = txtBox.WordWrap;
Finished!
Don't forget that I have included the source files
Notes_Extend_.zip ( 70.62k )
Number of downloads: 188This post has been edited by gbertoli3: 27 Aug, 2008 - 06:51 PM