Thursday, June 16, 2011

How to make Own notepad in Visual C sharp

Make Your Own Notepad
In this tutorial we will be making a simple C# Notepad with the option to Clear, Cut, Copy, Paste, Select All, Save, Open, and exit. I am using Visual Studio 2008 to make this tutorial, but you can use any C# editor that you like, although, we will be making a GUI so you probably want to use either Visual Studio or Visual C# to follow.
 All right, so now we can start making the GUI.

Step 1: Starting a new project.
To start a new project, goto File -> New Project -> Windows Form Application and name it whatever you want.

 You should get a blank GUI named Form1, if you do goto Step 2, if not post a reply and tell me what your problem is, I am glad to help.
Step 2: Making the GUI

For this Notepad application we are going to be using a RichTextBox for the editor. So, goto your Toolbox and select richTextBox and put it onto your application form (Make sure to leave room at the top for the menu).

 All right, now you have your editor area, but we need to make a menu for the people using your application to use some features, so add a MenuStrip and type in the first box "File". Then there will be a drop down box to add more stuff, add "New", then click the little down arrow on the next one and click Seperator, this will make a line seperating the different menu options. After the seperator add "Save File" and then "Open File", then a seperator, then "Exit".

 All right, now you have your File menu, now next to File make another called "Edit", then for this one add "Cut", "Copy", and "Paste", and Select All to the Edit menu.

 There, now your menu is finished, onto Step 3 where we finally start coding!

Step 3: Adding events to the menu
All right, so now we have our GUI, we can run it but the buttons don't do anything. That is because we have not programmed in the events for these buttons yet, now this might sound difficult to some of you, but Visual Studio and Visual C# make it very easy to do this, as it makes the event command for us, we just have to tell it what to do, so double click on the "New" menu item under the file menu and it should take you to some code, your code should look something like this,

Code:
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;
namespace Notepad

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void newToolStripMenuItem_Click(object sender, EventArgs e)

        {

             // This is where the code for the button click of "New" is going to go.

         }

    }

}

Ok, so now under the private void newToolStripMenuItem_Click we need to add this code,

Code:

richTextBox1.Clear();


This basically calls the richTextBox1 which is the name of the richTextBox in your form, and tells it to Clear. This code is really simple and most of the commands for the buttons will look sorta like this.

 So now your code should look like so,

Code:

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;



namespace Notepad

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void newToolStripMenuItem_Click(object sender, EventArgs e)

        {

             richTextBox1.Clear();

        }

    }

}



Now, run your project and type something in, then click the new menu button and see if it clears the text, if it does great, but if it doesn't don't get discouraged, just reply and I will be sure to tell you your problem.

 Ok, now let's make the exit button code, I know we are skipping Open and Save, but they are the hardest to grasp, so we will do those last, in C# it doesn't matter in what order you code your buttons as long as there is a click command for them, they will work.

 For the Exit button, it is extremly simple, it is just simply

Code:

 Close();

 so just double click the exit button and for the code just type in Close();

For the Cut, Copy, Paste, and Select All buttons, they are all pretty much the same as the new button, but with different command, but still referring back to the richTextBox.

 So, I am going to list the code for each button, just double click the right button and add the right code for that button command, it can't get much easier than copy and paste.

 Cut:

Code:

richTextBox1.Cut();

Copy:

Code:

richTextBox1.Copy();

Paste:

Code:

richTextBox1.Paste();

Select All:

Code:

richTextBox1.SelectAll();

Ok, so now just save and run your program and see if everything is working so far, if so, carry on, if not leave a reply and I will get back to you.

 Ok, now for the hardest part, the Save and Open options, go back into your GUI form and add a openFileDialog, and saveFileDialog. It doesn't matter where you place them on the form, they will just be put at the bottom for easy placement.

 Ok, now double click on the saveFileDialog, as we will do that first.

Now, the code for this is,

Code:

 saveFileDialog1.Filter = "Text Document (.txt)|*.txt|Rich Text Document (.rtf)|All Files (*.*)|*.*";

             saveFileDialog1.Title = "Save File";

            saveFileDialog1.OverwritePrompt = true;



            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

             {

                richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);

             }

Now, this can look like alot of code for beginners, but if you look at it logically, it is really quite simple.

 Code:

saveFileDialog1.Filter = "Text Document (.txt)|*.txt|Rich Text Document (.rtf)|All Files (*.*)|*.*";



 this is the part most people have trouble grasping. Basically what we are doing is setting the Save As Type: in the save file dialog box. So, if you goto Notepad (the real one with Windows) then when you click Save, you get a little thing down at the bottom that says "Save as type:" and you can click which one you want, that is what we are setting here. We are saying that the user can save a file as a .txt Text Document, a .rtf Rich Text Document, or they can specify there own .whatever extension.

The "|" seperate the different things it can be, like "Text Document (.txt) is what shows up when the user is looking at it, but the *.txt is what the program is going to save it as, * as a wildcard for the name they choose, and .txt as the extension. Same for the rest of them.



[code[saveFileDialog1.Title = "Save File";

saveFileDialog1.OverwritePrompt = true;[/code]



This is setting the title of the Save File prompt to Save File and making the Save File prompt the user if he/she has to overwrite a file or not. If you do not put overwrite prompt as true, it is false by default and it will overwrite the file without asking the user. Not smart.

 If you do not know what if statements are, they are basically a way to check to see if a certain condition is true, based on a given statement, so the code,

 Code:

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

            {

                richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.RichText);

             }

Is basically asking the user (I am going to put this into human words so you can understand it) if they click the OK button on the save file dialog box then save the file that they have typed up.

 And that is it, if you wanted to you could add some error handling, but we will save that for another tutorial.

 So, lets do the open dialog, which is almost the same as the save file dialog, but with minor differences. I will point them out in the code.

 Code:

openFileDialog1.Title = "Open File";

            openFileDialog1.Filter = "Text Document (.txt)|*.txt|Rich Text Document (.rtf)|All Files (*.*)|*.*";

             // There is no overwrite prompt, because we don't need one if we are opening a file.



            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)

             {

                richTextBox1.LoadFile(openFileDialog1.FileName);

                // This is a little different because we are just opening a file, not sa10:07 AM 6/17/2011ving it, so there are not as many parameters.

             }

Let me go over just this little bit of code and we are finished with the tutorial,

 Code:

richTextBox.LoadFile(openFileDialog.FileName);

This basically says, once again referring back to richTextbox1, Load the file name that the user has typed in or clicked on. Simple, yet effective.

 All right, so that is it for this tutorial, just leave a reply if you want me to make more tutorials or if you have any problems.

 Also, I am going to make another tutorial on how to do this exact same thing in VB.NET later so get ready for some more tutorials.

 Thanks for reading (I am not just going to give you the source, because this is a tutorial and that wouldn't help you learn anything),

Gulshan- Arora Community

4 comments:

  1. Thank You So Much.
    I have Created my Save Open and Cut copy commands in my own Notepad.
    Your codings are very easy and understandable.

    but m still facing problem in creating Replace and Find. Will u plz frwd it's Code as soon as Possible.

    ReplyDelete
  2. This is splended thanks alot and alot again :)

    ReplyDelete
  3. Error 1 The name 'OpenFileDialog1' does not exist in the current context

    how to fix this??

    ReplyDelete
  4. Just drag and drop OpenFileDialog from Tool box

    ReplyDelete

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...