Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,248 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,201 people online right now. Registration is fast and FREE... Join Now!




Creating your own Notepad App

2 Pages V  1 2 >  
Reply to this topicStart new topic

> Creating your own Notepad App

gbertoli3
Group Icon



post 20 Aug, 2008 - 07:55 PM
Post #1


This is a Two-Part Tutorial on Creating and Extending the Notepadd App.

Create a Notepad App
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.
IPB Image

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.
IPB Image

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

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: Attached File  Notes.zip ( 63.59k ) Number of downloads: 89


Extend the Notepad App

In 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
IPB Image

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+N
For the Open menu item's shortcut key type in Ctrl+O
For the Save menu item's shortcut key type in Ctrl+S
For the Save As menu item's shortcut key type in Ctrl+Shift+S
For the Print menu item's shortcut key type in Ctrl+P
For the Print Preview menu item's shortcut key type in Ctrl+Shift+P
For the Undo menu item's shortcut key type in Ctrl+Z
For the Cut menu item's shortcut key type in Ctrl+X
For the Copy menu item's shortcut key type in Ctrl+C
For the Paste menu item's shortcut key type in Ctrl+V
For the Select All menu item's shortcut key type in Ctrl+A
For the Font menu item's shortcut key type in Alt+F


Now we will add a StatusStrip()
IPB Image

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 Attached File  Notes_Extend_.zip ( 70.62k ) Number of downloads: 188


This post has been edited by gbertoli3: 27 Aug, 2008 - 06:51 PM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

c.v.burgess
*



post 20 Sep, 2008 - 11:25 AM
Post #2
i get an error "file name cannot be empty" when opening or saving in yoUR app and mine. What's the problem?
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 20 Sep, 2008 - 11:51 AM
Post #3
I don't know can you post a screenshot of the ErrorMsg
Go to the top of the page
+Quote Post

c.v.burgess
*



post 20 Sep, 2008 - 12:20 PM
Post #4
Attached Image
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 20 Sep, 2008 - 12:22 PM
Post #5
What were you doing to get that message?
Go to the top of the page
+Quote Post

c.v.burgess
*



post 20 Sep, 2008 - 12:27 PM
Post #6
Attached Image

that is opening a file
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 20 Sep, 2008 - 12:30 PM
Post #7
That's Weird!

Did you make sure that the file exists.

Go to the openFileDialog's Properties and make sure the CheckFileExists Items is set to True
Go to the top of the page
+Quote Post

c.v.burgess
*



post 20 Sep, 2008 - 12:37 PM
Post #8
QUOTE(gbertoli3 @ 20 Sep, 2008 - 01:30 PM) *

That's Weird!

Did you make sure that the file exists.

Go to the openFileDialog's Properties and make sure the CheckFileExists Items is set to True

i did
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 20 Sep, 2008 - 12:48 PM
Post #9
That's really weird. Did you make any modifications to the project?
Go to the top of the page
+Quote Post

c.v.burgess
*



post 20 Sep, 2008 - 01:00 PM
Post #10
QUOTE(gbertoli3 @ 20 Sep, 2008 - 01:48 PM) *

That's really weird. Did you make any modifications to the project?

none WHATSOEVER
Go to the top of the page
+Quote Post

gbertoli3
Group Icon



post 20 Sep, 2008 - 01:11 PM
Post #11
That is really weird because it works for me fine. The only thing I can think of doing is redownload the project.
Go to the top of the page
+Quote Post

c.v.burgess
*



post 21 Sep, 2008 - 06:50 AM
Post #12
this works for me (save file)
CODE
private void button1_Click(object sender, System.EventArgs e)
{
     Stream myStream;
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
     saveFileDialog1.FilterIndex = 2;
     saveFileDialog1.RestoreDirectory = true;

     if(saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         if((myStream = saveFileDialog1.OpenFile()) != null)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
}


but will u tell me what to put for "//Code to write the strream goes here"? thank you

This post has been edited by c.v.burgess: 21 Sep, 2008 - 07:04 AM
Go to the top of the page
+Quote Post


2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 04:03AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month