C# 2005 Basics 1

This is where the Admins will put tutorials submitted by users.
Post Reply
[users]Prodigy





Posts: 1
Joined: Sun Aug 13, 2006 8:17 am

C# 2005 Basics 1

Post by [users]Prodigy »

C# 2005 Basics 1

Well as we all know everyones gotta start somewhere, so this tutorial is aimed to get you going with CSharp,
Get it free here -> Visual C# 2005 Express Edition

One thing i would like to say first is that because of what we are making the program in, anyone who wants to use the program will need .net framework 2.0 available here.. otherwise they will get an error.

To start a new project click Image found in the topleft corner.

Youll now see something like this:
Image

These are the different types of application you can make, in this tutorial we'll be using Windows Application; so select that. On doing so you'll notice that in the textbox below the words WindowsApplication1 will come up; this will be the name of your project, as we will be making an organiser in this tutorial change it to Organiser and click OK.

The designer will now come up, this is where you can visually edit your program.
The "form" you see is your program:
Image

You should also see the Properties window:
Image

A Solution Explorer:
Image

And the Error, Warning and Message lists window:
Image

All these windows can be moved around and resized to your liking with the mouse.

You may have also noticied that each window can also be closed, if at anytime you wish to reopen a window just open the view menu at the top and select the window from the dropdown:
Image

Saving and Distributing your program
1st of all i will tell you how to save your app, around the top-left corner you should see a little button that looks like this:
Image

Click it and this window should appear:
Image

The 1st box is the name of your project,
The 2nd box is the location it saves too
and the 3rd box is the name of your *solution.

* A solution is basically a list of your projects, in our app we have one project; this is quite normal as its quite rare to need more than one.

Once your happy with the information youve entered click OK to finalise the save.

Note: If Image is checked and the location you enter doesnt exist then it will be made.

Now whenever you wish to save just click Image

IN order to distribute our app we will first need to build it, this is rather easy as all you need to do is press F6 ;) ... in order to build+debug the app press F5.

Alternatively you could enter debugging mode through the debug menu:
Image

So now you will see your program floating there! Of course its nothing at the moment as all we've done is save it, but its a start B)

You can exit debugging mode by pressing the X on your app or through the debug menu again:
Image

So now your program has been built, lets go get it:
Open up the folder you saved your project to -> Go into the next folder -> Bin -> Debug; here you will see your built app:
Image
It is remade everytime you build.

Note: You wont need to worry about the other files in this folder as they're just debugging things, although if you know that your app relys on something in there you will have to remember to keep them together.

Customisation
So the icon isnt exactly totally radically huh? Well we better fix that then! Go back into c# and rightclick your projects properties in the Solution Explorer and select open:
Image

Another page will now come up with around 10tabs on the left, for now we'll just be worrying about the 1st tab.

Just to name a few things: the Assembly Name is the name of your app and the Root Namespace is the name the project refers to the app as.

To change the icon click the button next to the icon combobox. You can now browse through your comp for a suitable icon for your app (The FastIcons download page offers some pretty good ones: http://www.fasticon.com/downloads.html)

Once chosen, save the app and build it again using F5; you can now go into the debug folder and feel awesome about your achievement :rolleyes:
Image

You'll notice that there is a line of text under the name that is slightly faded; it is possible to change that and furthermore add another line of text under that by clickng the Assembly Information... button... we will not be getting into this though as it is beyond the scope of the tutorial and, well, not really needed.

Right, time to actually begin coding!
Ill try and make this as easy as possible: 1st off go back to your designer tab (should be on the bar of tabs near the top) and open up the toolbox by pressing Image (Should be around the topright)

So now a list of tools should make themselves apparent, as you can see there are loads of tools, all we will be using though is the button so click 'n' drag it onto the form:
Image

For this tutorial we want a txt document to open up whenever we press the button, so go make a txt document called hello.txt and save it into the debug folder; now double-click the button.. welcome to the code page for form1 :!:

You should see this:

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Organiser
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{

		}
	}
}
Pretty confusin lookin huh? Well let me break it down for you and explain it bit by bit:

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
This first bit is what the app 'uses'.. basically it just makes writing code quicker, more on this later.

Code: Select all

namespace Organiser
{
The word following "namespace" is the name of the project, nothing hard about that, the bracket on the next line is not to hard to understand either, basically code is always enclosed with opening and closing brackets {}. If you click just infront of the bracket you will find it highlights in gray, the bracket it is one with is also highlighted in gray. This is useful for obvious reasons for finding out where each one begins and ends.

Code: Select all

	public partial class Form1 : Form
	{
This defines a class in which all the code for the form will be in, the word following "class" is the name of the class and the word following the : is its type, in this case we are a form.

Code: Select all

		public Form1()
		{
			InitializeComponent();
		}
the word following "public" will always be the name that followed "public partial class", basically code within here happens before the form is seen. "InitializeComponent()" calls an event which will place everything you did in the designer on the form (e.g. the button).

Code: Select all

		private void button1_Click(object sender, EventArgs e)
		{

		}
Thee above code was generated when you double-clicked the button, it is what is know as a handle. Every tool has a default handle, the button is .click. Anything that goes within the brackets here will happen when the button is clicked, simple huh? To see a full list of a tools handles go back into the designer -> select the tool on the form -> click the Image found at the top of the properties box and you will see them.

Code: Select all

	}
}
These are the closing brackets for the "public partial class" and the "namespace".

In order to launch hello.txt when we click the button we will need to do the following:
First of all type Process within the "button1_click" brackets; you will notice as soon as you begin to type a list comes up; this is known as intellisense and lists everything you can type at that moment in time. Ignor it for now. Once youve typed Process you will notice the last s has a redline underneath it, hold your mouse over it and click the arrow that appears:
Image

If you were to select the 1st option, the code "using System.Diagnostics;" would be added to your list of uses, the 2nd option would replace Process with "System.Diagnostics.Process".

You might be wondering what is the point in all this, well, the point is Process is a part of System.Diagnostics, and while the app can use System.Diagnostics, it wasnt using it at the time. The best course of action to take here is to choose the 1st option as, like i said before, it just makes writing code quicker as you will only have to type Process instead of System.Diagnostics.Process.

Back to coding... after Process add .Start(Application.StartupPath + "\\hello.txt"); so your code should now look like this:

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Organiser
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Process.Start(Application.StartupPath + "\\hello.txt");
		}
	}
}
Run the app, press the button, rejoice as the txt document opens :D

Now then i'll explain what i just did:

Code: Select all

.Start
Tells the app to start whatever filepath that is in the brackets next to it

Code: Select all

(Application.StartupPath
This will return the folders filepath that the app is within

Code: Select all

 + "\\hello.txt");
The + joins the 2sentences together, sentences are always enclosed in speechmarks, they are not known as sentences either, but strings. The \\ will come out as just \. The reason we put 2 slashes was because if you just put one on its own c# reads what follows it differently, this is something we'll get onto at a later date. the semi-colon at the end signifies the end of that line of code, you always put a semi-colon at the end.

Tahts it for now :P , mayb more soon, until then post any questions you may have and i'll be happy to answer.
baseball1332





Posts: 180
Joined: Mon Jan 16, 2006 9:07 pm
Location: MIA 305!!

Post by baseball1332 »

Nice...Great for begginers!
I wouldn't mind seeing a more advanced version! :wink: :wink:
User avatar
angry man





Posts: 492
Joined: Fri May 19, 2006 5:51 pm

Post by angry man »

I might get into programming just cuz this looks so interesting! TYVM!
Image
I Can See Inside Your Monitor :lol:
Post Reply