[Tutorial] Programming a Simple Text Editor in C#

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
PlasmaGhost





Posts: 149
Joined: Wed Oct 05, 2005 12:23 pm

[Tutorial] Programming a Simple Text Editor in C#

Post by PlasmaGhost »

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 :wink: ... 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 = ""; }
Now go back to the designer and double click on the Open... sub object. Type this code in betweent he { and }:

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 { }
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:

Code: Select all

private String cFile = "";
Add this code between the brackets:

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 { }
}
Go back to the designer and double click on Save As... and place this code betweent he brackets:

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 { }
And the last code for this step! Double click on Exit and add this betweent he brackets:

Code: Select all

Close();
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:

Code: Select all

t.Undo();
Simple! Now double click on Cut and add this code between the brackets:

Code: Select all

t.Cut();
Easy enough. Double click on Copy and add this code between the brackets:

Code: Select all

t.Copy();
Double click on Paste and add this code between the brackets:

Code: Select all

t.Paste();
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:

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;
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:

Code: Select all

ColorDialog c = new ColorDialog();
c.AnyColor = true;
c.AllowFullOpen = true;
c.FullOpen = true;
c.ShowDialog();
t.ForeColor = c.Color;
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:

Code: Select all

t.SelectAll();
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
superaison





Posts: 175
Joined: Mon Jul 25, 2005 5:06 pm

Post by superaison »

Nice tut, once i understand VB, C# is next. Then c++
PlasmaGhost





Posts: 149
Joined: Wed Oct 05, 2005 12:23 pm

Post by PlasmaGhost »

once you have VB done, there are very few things u need to do to learn C#. The hardest being from Subs to Voids, from functions to acutal types and the brackets {} and the lines ending with a ;. thats pretty much it :P
PlasmaGhost





Posts: 149
Joined: Wed Oct 05, 2005 12:23 pm

Post by PlasmaGhost »

do ppl not appreciate good tuts? please comments!
Spartan III




Wordewatician 500

Posts: 663
Joined: Tue Nov 11, 2003 7:07 am
Location: Manchester, United Kingdom

Post by Spartan III »

Nice job. When I get my mitts on C#, I'll give it a go. Keep it up.
LiquidEnforcer





Posts: 10
Joined: Mon Sep 05, 2005 10:15 pm

Post by LiquidEnforcer »

orrr.... you could learn C++ and automatically have everything you will need to easily pick up any other language... You cant know Java and pick up a C++ book and learn it in a weekend. However you can know C++ and learn java in a weekend. Replace Java with any other language. Except for maybe assembly, although its not that hard if you have a good background with C++.

Between C# and VB they are both pretty much the same but C# steals C++'s syntax. So if anything get good with both C++ and C# as they both have the same general feel, but separate and different uses.
LiquidEnforcer





Posts: 10
Joined: Mon Sep 05, 2005 10:15 pm

Post by LiquidEnforcer »

Let me know what this does and you get a cookie ;)

Code: Select all

int bang(int x)
{
	x = (x >> 16) | x;
	x = (x >> 8) | x;
	x = (x >> 4) | x;
	x = (x >> 2) | x;
	x = (x >> 1) | x ;
	x = ~x & 1;

	return x;
}
Knowing the organisation and arcitecture of computers in general is very important if you are at all serious about learning C++ and actually understanding it.
PlasmaGhost





Posts: 149
Joined: Wed Oct 05, 2005 12:23 pm

Post by PlasmaGhost »

wow if ur so skilled with C++ then why are u even looking at my tutorial? so then all of the ppl out there who dont spend they're life on computers should use C++? i program for fun not to piss ppl off with my mad computer skillz (haha).
LiquidEnforcer





Posts: 10
Joined: Mon Sep 05, 2005 10:15 pm

Post by LiquidEnforcer »

I think Ill just quote myself...
LiquidEnforcer wrote:Knowing the organisation and arcitecture of computers in general is very important if you are at all serious about learning C++ and actually understanding it.

Just trying to help, dont blow an ovary.
chiefdestroyer





Posts: 13
Joined: Thu Mar 03, 2005 7:53 pm

programming

Post by chiefdestroyer »

i agreee with LiguidEnforcer if you learn c++ you can learn anything i have not learnt c++ but i have looked at it i personal don't think i want to learn it i have also kinda learnt VB and have learnt c# and am a very good programmer at java i reckon java is a much better langauge cause it is very proper and very constant were as c# is microsofts rip off of java but not as proper or as constant as java and i don't really like visual studio 2005 or 2003 cause it is not a great set up for doing alot of hand coding eclipse is much better for that

PlasmaGhost why do you have a cry when you do not get a response that you were expecting!!!
LiquidEnforcer





Posts: 10
Joined: Mon Sep 05, 2005 10:15 pm

Post by LiquidEnforcer »

I would actually say C# is a merge of VB.NET and C++. Java is ok but its not efficient at all :/

VS2005 blows, has way too many bugs and performance issues. VS2003 with Visual Assist is the way to go.
chiefdestroyer





Posts: 13
Joined: Thu Mar 03, 2005 7:53 pm

Post by chiefdestroyer »

ok
alot of people say that but i don't really see how i know it's swing components are very efficient on memory but other than that i reckon it is pretty average not that i have been comparing i guess it also depends on what you are doing with it but i like it cause it is so proper and you can just about guess things that you do not know how they work

LiquidEnforcer do you do much programming in java
LiquidEnforcer





Posts: 10
Joined: Mon Sep 05, 2005 10:15 pm

Post by LiquidEnforcer »

Really no actual programming in java on my part. But I know what java is, its a language that consists of a script executed at run time. C++ is precompiled into machine code and then distributed to be ran on a target system. Just that fact alone makes java multiple orders of magnitude slower than C++. Don't know the exact numbers on the fly.

I also happen to have knowledge of Intel Assembly language. ASM Is an extremely low level language and a pain in the ass as well. Trust me when I say I know what im talking about.
chiefdestroyer





Posts: 13
Joined: Thu Mar 03, 2005 7:53 pm

Post by chiefdestroyer »

dam i thought i have found another java programmer that was interested in halo cause it would be really handy to know someone else that could help with some of my projects not really how to do stuff help more cutting the project size in half help and would be extremly handy for a idea of a project that i have if i get the idea accepted by some other people i know
Klaz0r





Posts: 49
Joined: Tue Jul 27, 2004 2:36 pm

Post by Klaz0r »

Thanks for the tut, I've been wanting to switch to C#, but there was never an initiative. Now I think I'll go buy a book on it or something :P. Also, yes, I agree, ASM = pain, don't ever try to make a program with that without a degree in rocket science.
theenjoisk8r





Posts: 125
Joined: Wed Mar 24, 2004 4:32 pm

Post by theenjoisk8r »

so ive never done any programming before, and a few days ago i decided to start learning C++. so far so good. ive learned everything from headers, to arrays so far. is this a good choice or would you suggest a different language first. which is harder, C++ or VB (which i hav never heard of before today)?
LiquidEnforcer





Posts: 10
Joined: Mon Sep 05, 2005 10:15 pm

Post by LiquidEnforcer »

Itreally depends on what you wanna do with it. If you learn C++ you will do very well at learning virtually any other language. If you arent serious at all about learning how to program and just want to mess around then I would probably suggest C#. If you want to know your stuff and possibly apply it to useful situations, C++ is definitely worth it.
theenjoisk8r





Posts: 125
Joined: Wed Mar 24, 2004 4:32 pm

Post by theenjoisk8r »

yeah im a sophmore (but i started coming to this site like, forever ago. 7th or 8th grade i think.) and i recently did an interview to get into an engineering class at a college for a class next year. one of the questions was if id ever designed any programs or anything like that. the only thing i could think of was modeling in gmax and some other programs (plus alot of other crap) but i was sad that i couldnt say i had programed anything. so i decided to learn. :D and i know i never would hav even attempted it had i not been active in this site. hilariously enough, modding halo has helped me with my long term plans for a career!

....how sad is that. O well!
LiquidEnforcer





Posts: 10
Joined: Mon Sep 05, 2005 10:15 pm

Post by LiquidEnforcer »

Playing around with modding/hacking Counter Strike: Source helped me out in the same respects.

I sounds to me like your pretty serious so I would go with C++. Because like I said before, learn C++ and all other come easily. Learn sometihng like C#, Java, or VB and its a much bigger transition to another language.
imgettionowned




Wordewatician 500

Posts: 709
Joined: Sat Jun 25, 2005 8:53 am

Post by imgettionowned »

you make it seem like learning a programming language such as C++ is an esay task.

It is the complete opposite!!!!!!!!!!!!!!!!!
Post Reply