[Tutorial] Programming a Simple Text Editor in C#
Posted: Sat Dec 17, 2005 8:41 am
Hello everyone. I'm trying to build a reputation by helping others and I think you people in Visual Basic deserve C#. C# my look hard at first glance but, if you have learned VB, you are ready for C#. I started VB about 2 months ago and about 1 month ago I started Java. A week later I started C# and I am now learning C++. Programming is not that hard, you just have to understand how exactly it works. What I plan to do in this tutorial is create a very simple text editor. So, lets get started!
Utilities Needed:
Visual C# 2005 (The beta might work with it)
Step 1:
Start up C#. If you can't find the program don't worry. Go into the install directory where you installed it to (C:\Program Files\Visual Studio 8\). Go into the folder called Common7. Then go into IDE. Look for a file with an icon of a green box with C# in it and a blue arrow under it. Right click it and pick Send to Desktop. You now have a desktop shortcut to get there. So close the folder and load up C#. It may take a bit so I'll wait ... Ok it's done! Now onto step 2!
Step 2:
Go to File -> New Project. Select Windows Application and change the name to "Text Editor" or whatever you want it called. Now wait until a window is displayed inside the editor with the title Form1.
Step 3:
If the toolbox is not displayed, go to View -> Toolbox. No from the toolbox, click the plus on the All Windows Forms. Go down and drag a menustrip anywhere onto the form. To add menu items, click the menustrip anywhere and click in the box that says Type Here. Type File and press enter. You have just created a Menu Object. Notice how it expands as if you clicked it on a real menu? If it didn't do this click the File menu object. No below the File menu object type New and press enter. Type Open... and press enter. Type Save and press enter. Type Save As... and press enter. Type Exit and press enter. Now where it says Type Here under the File Menu object and below all of them Sub Objects you just typed, press the little arrow on the right of it and pick separator. Create another. Drag one onto Save and it will be placed above it. No drag the other on onto Exit to place it below Save As... so now the order should be: New, Open..., Separator, Save, Save As..., Separator, Exit.
Step 4:
You may want to go to File -> Save Project so your work is preserved. We will now add an Edit menu. So next to File add Edit. Below Edit add Undo, Cut, Copy, Paste, Font..., Font Color..., and Select All. Create 3 separators and put them in this order: Undo, Separator, Cut, Copy, Paste, Separator, Font..., Font Color..., Separator, Select All.
Step 5:
We will now add a TextBox control to the Form. To do this, drag a text box from the Toolbox onto the form. If there is no properties box showing, right click on the TextBox and click Properties. Now change the name to "t" without the "" (duh!). Change the Dock to Fill. Its the center button when you click the dropdown arrow. Change Multiline to true. Notice how the textbox takes the shape of the form. Changed the Dock to Fill does this but changing Multiline to True lets it expand on the Y axis. One last thing: click the Form caption, change the Text property from Form1 to "Text Editor". Now lets begin coding!
Step 6:
Double click on the File -> New in your form. Enter this code in between the { and the }:
Now go back to the designer and double click on the Open... sub object. Type this code in betweent he { and }:
That will open a text file. You may want to save now just in case. Now go back to the designer and double click on Save. Above the private void for the Save you just created, add this:
Add this code between the brackets:
Go back to the designer and double click on Save As... and place this code betweent he brackets:
And the last code for this step! Double click on Exit and add this betweent he brackets:
Phew! That step is done! The rest of the code isn't near as long as that!
Step 7:
Double click on Undo and add this code between the brackets:
Simple! Now double click on Cut and add this code between the brackets:
Easy enough. Double click on Copy and add this code between the brackets:
Double click on Paste and add this code between the brackets:
Now these next to codes won't be big but will be bigger than the 1 line codes. so double click on Font... and add this code between the brackets:
That's not so difficult. Well let's add the ability to change the font color. The reason I disabled the ShowColor function in the Font Dialog is because you can get MANY more colors in a color dialog. So double click font color and here's the code to insert betweent he brackets:
That wasn't so bad now was it! Now to finish off the Edit menu, double click on Select All and add this betweent he brackets:
Ok! That's it. Expect part 2 soon. Part 2 will turn this sucker into and MDI form with more menus and more code.
Note: I made this without the Visual C# 2005 open, so there may be errors. If there are, please notify me in this topic and I will fix them ASAP.
~PlasmaGhost
Utilities Needed:
Visual C# 2005 (The beta might work with it)
Step 1:
Start up C#. If you can't find the program don't worry. Go into the install directory where you installed it to (C:\Program Files\Visual Studio 8\). Go into the folder called Common7. Then go into IDE. Look for a file with an icon of a green box with C# in it and a blue arrow under it. Right click it and pick Send to Desktop. You now have a desktop shortcut to get there. So close the folder and load up C#. It may take a bit so I'll wait ... Ok it's done! Now onto step 2!
Step 2:
Go to File -> New Project. Select Windows Application and change the name to "Text Editor" or whatever you want it called. Now wait until a window is displayed inside the editor with the title Form1.
Step 3:
If the toolbox is not displayed, go to View -> Toolbox. No from the toolbox, click the plus on the All Windows Forms. Go down and drag a menustrip anywhere onto the form. To add menu items, click the menustrip anywhere and click in the box that says Type Here. Type File and press enter. You have just created a Menu Object. Notice how it expands as if you clicked it on a real menu? If it didn't do this click the File menu object. No below the File menu object type New and press enter. Type Open... and press enter. Type Save and press enter. Type Save As... and press enter. Type Exit and press enter. Now where it says Type Here under the File Menu object and below all of them Sub Objects you just typed, press the little arrow on the right of it and pick separator. Create another. Drag one onto Save and it will be placed above it. No drag the other on onto Exit to place it below Save As... so now the order should be: New, Open..., Separator, Save, Save As..., Separator, Exit.
Step 4:
You may want to go to File -> Save Project so your work is preserved. We will now add an Edit menu. So next to File add Edit. Below Edit add Undo, Cut, Copy, Paste, Font..., Font Color..., and Select All. Create 3 separators and put them in this order: Undo, Separator, Cut, Copy, Paste, Separator, Font..., Font Color..., Separator, Select All.
Step 5:
We will now add a TextBox control to the Form. To do this, drag a text box from the Toolbox onto the form. If there is no properties box showing, right click on the TextBox and click Properties. Now change the name to "t" without the "" (duh!). Change the Dock to Fill. Its the center button when you click the dropdown arrow. Change Multiline to true. Notice how the textbox takes the shape of the form. Changed the Dock to Fill does this but changing Multiline to True lets it expand on the Y axis. One last thing: click the Form caption, change the Text property from Form1 to "Text Editor". Now lets begin coding!
Step 6:
Double click on the File -> New in your form. Enter this code in between the { and the }:
Code: Select all
DialogResult res;
res = MessageBox.Show("Do you wish to clear the textbox and lose unsaved changes?", "Powered by PlasmaGhost", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (res == DialogResult.Yes) { t.Clear(); cFile = ""; }
Code: Select all
OpenFileDialog o = new OpenFileDialog();
o.Filter = "Text Files (*.txt)|*.txt";
o.Multiselect = false;
o.Title = "Powered by PlasmaGhost";
o.ShowDialog();
try
{
o.OpenFile();
System.IO.StreamReader sr;
sr = System.IO.File.OpenText(o.FileName);
t.Text = sr.ReadToEnd();
cFile = o.FileName;
}
catch { }
Code: Select all
private String cFile = "";
Code: Select all
if (cFile == "")
{
SaveFileDialog s = new SaveFileDialog();
s.Filter = "Text Files (*.txt)|*.txt";
s.Title = "Powered by PlasmaGhost";
s.ShowDialog();
try
{
System.IO.StreamWriter sw;
sw = System.IO.File.CreateText(s.FileName);
sw.Write(t.Text);
sw.Flush();
cFile = s.FileName;
}
catch { }
}
else
{
try
{
System.IO.StreamWriter sw;
sw = System.IO.File.CreateText(cFile);
sw.Write(t.Text);
sw.Flush();
}
catch { }
}
Code: Select all
SaveFileDialog s = new SaveFileDialog();
s.Filter = "Text Files (*.txt)|*.txt";
s.Title = "Powered by PlasmaGhost";
s.ShowDialog();
try
{
System.IO.StreamWriter sw;
sw = System.IO.File.CreateText(s.FileName);
sw.Write(t.Text);
sw.Flush();
cFile = s.FileName;
}
catch { }
Code: Select all
Close();
Step 7:
Double click on Undo and add this code between the brackets:
Code: Select all
t.Undo();
Code: Select all
t.Cut();
Code: Select all
t.Copy();
Code: Select all
t.Paste();
Code: Select all
FontDialog f = new FontDialog();
f.AllowScriptChange = false;
f.AllowVerticalFonts = false;
f.ShowColor = false;
f.ShowEffects = true;
f.ShowDialog();
t.Font = f.Font;
Code: Select all
ColorDialog c = new ColorDialog();
c.AnyColor = true;
c.AllowFullOpen = true;
c.FullOpen = true;
c.ShowDialog();
t.ForeColor = c.Color;
Code: Select all
t.SelectAll();
Note: I made this without the Visual C# 2005 open, so there may be errors. If there are, please notify me in this topic and I will fix them ASAP.
~PlasmaGhost