You are on page 1of 8

C# Tutorial 1: the cooler version of “hello world”

If this is your first language that is computer related which you are learning, then
you have come to the right place. This tutorial is being written while I am currently
a 15 year old so I don’t need to really try in keeping it as simple as I can. Anyway, in
this first tutorial, we will be going though our first program and the structure in c#.
If you haven’t learned already (which I assume you haven’t), C# needs to have semi
colons in every line which has a command so that will be interesting when you start
debugging (the process of removing bugs/errors in your program) which you find
that the whole program doesn’t compile because of a small semicolon. Anyway,
let’s get started doing the preliminary stuff.

First thing is to make sure you have the right compiler for your operating system.
Most people have a windows operating system and it is usually of Windows xp,
vista, or windows 7. If you are unfamiliar with what any of these are, I strongly
advise you to check back again another time (I may be a simple talker but you
really need to know what an operating system really is before you touch making
programs for it). The compiler which I will use for this tutorial will be Visual Studio
2010. You can get free “express editions” of this at:
“http://www.microsoft.com/express/” and it is free for a lifetime because Microsoft
was just nice that way ;)….So get hold of the software from there and download it to
whatever computer you are using to make programs. It is actually a fairly big
program so I would just leave that time for a nice cup of hot coffee while it finishes
downloading.

Once you have visual studio express edition C# done downloading, you can open It
by going to Start>All programs>Visual Studio>Visual Studio 2010 Express Edition
on windows xp and it is a similar procedure on other operating systems as well (as
long as you are using windows). If you are stuck with a mac, you need to get
yourself a windows before you start coding…or if you are super duper advanced and
want to take a step up, look at the mono project for mac. It let’s you somehow
run .net compilers on Macs.

The first time you load up the application, it may be a little slow with a green
progress bar scrolling around and once it’s loaded, the interface will look like below,
except it will say express edition instead of the professional edition (which is what I
am using). The pro edition is way more expensive but it won’t have any extra
features which I will go over in the following tutorials…maybe for some advanced
ones in the future.
*pic1*

Now, navigate to the FILE menu on top and click on NEW>PROJECT. This will open
up the new project dialog box like shown.

*pic 2*

Now, you need to navigate the Visual C# section on the left and then click on the
“Windows Forms Application” as the project type and we will today be making a
simple ‘hello world button’ except unlike the boring old fashioned hello world
projects which tutorials typically take you through, we will be redefining the world of
hello world (no pun intended).

Next, you need to choose a name for the project and a location to store the project
files, like so:

*pic 3*

After that, once you are comfortable with the file settings, just click the ‘ok’ button
and you are ready to go.

First thing you would probably notice is the ‘form’ which is created. You can imagine
a form as being a piece of paper. When you open up applications, these ‘windows’
which you can drag around and minimize are called forms. Just to make sure, verify
that your window looks like mine over below:

*pic 5*

Once you are satisfied with your doubts lingering in your head, you can go ahead
and look at the “PROPERTIES PANEL”. Now, the fact that I put it in quotes,
underlined it, and I bolded + italicized it probably means that we will use that magic
word again in the future. So, if you find it yourself then you are great for a newbie
but if you don’t, don’t fret because the picture below should highlight that:

*pic6*
Once you open your “PROPERTIES PANEL”, you should be able to look at the
properties of the window and whatever is in your program. Now, if you don’t know
what a property is, (as I didn’t when I was 13 or 12), it’s like when people have RED
HAIR, or BIG LEGS, or just UGLY POSTURE. These are things which are automatically
defined when you look at some people and just like that, in the properties panel,
you can choose if you want your window to have RED HAIR, or BIG LEGS, or UGLY
POSTURE. Firstly, scroll to the “name” property. This property should be clear
enough but there are some limitations. Your name can only have letters and
numbers. No spaces at all (like web addresses) . This is because if there is a space
in the name, the computer won’t know whether you are talking about 2 people or
just a guy with a first and last name. By default, the name is called form1…which is
really boring compared to the name: “SUPER_DUPER_AWESOME_FORM_1”, which if
you don’t want to spell out, can call “Dave”. So, we want Dave to be more than just
a gray boring form with nothing in him so let’s change some stuff about him. Go to
the backcolor property and you will find this little Carrot facing down:

*pic 7*

Click on this carrot and you can choose you color you want Dave to have as a
background..i chose “HotTrack” in the web section because I felt like I liked that
color. Anyway, after experimenting a ton and getting all excited, you can move on
to the next customization. Next, go to the “Text” property and change it to
whatever you want. This property will change the title text of the form. I decided to
make it “THIS IS A TITLE” (very unoriginal) but I am writing this at 12:00AM so im
not feeling very original at the moment. Anyway, moving on…

*pic 8*

Now, we have done so much makeover to dave yet he is still bland without any
buttons and cool stuff to do…so, lets add some cool stuff from the “TOOLBOX”,
again I did the fancy caps thing to emphasize how important this really is. The pic
below will show me pointing to the toolbox:

*pic9*

Now, we go to the “All windows forms controls” tab and we double click on the
“button” control. Basically, a control is just a “thing”. I know this sounds vague but
you will realize later that controls are basically things which the user (or whoever
wants this lame app) interfaces with…and a button is just one of the ways a person
can interface with a program. For now, we are going to be making it so when the
user clicks on the button, it should say “Hello USER!”. So, this is actually fairly
simple but before we start to code, you can actually drag around the control in the
form and start aligning it wherever you want to align it. Next, you can also change
it’s properties just like we changed the form’s properties…and this is via the…(I
hope you still remember)…PROPERTIES PANEL!. You can mainly just change the
“text” property of it…I wouldn’t’ really mess with the other properties, except for
the color properties.

*Make sure to rename the button to something else using the NAME property. Call it
something like “btnHello”. And change the TEXT property to something like “Click
me” or “Pull my finger” (if you know where I’m going with this…he he).

*pic10*

So far, you app should look like above, with the color a little different and the text in
the button possibly a little different. However…for the following code, we need to
know the name of what we called our button (so remember that or write it down on
a piece of paper).

If you want to access the default handler for a control (which for a button is called
“button press”), you simply, double click on the button and it takes you to the code
which takes place when you press a button. Now, you will get surely confused by
the functions and the premade code which Visual Studio made for you but let me
explain that all this code is just a “framework”. Now, where you pointer is right now
shows what happens when user clicks the button. In our case, we want the text for
the button to change to something else and surprise the user. So… let’s get started.
Below, I have copied the default code…in case you messed around with it and killed
it. You however need to replace “Dave” with whatever the name of your form is
(which I advised to call “dave”).

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

namespace Helloworld
{
public partial class Dave : Form
{
public Dave()
{
InitializeComponent();
}

private void btnHello_Click(object sender, EventArgs e)


{

}
}
}

Now, let’s forget about the code there and let’s just start making new code. If you
find the function called “btnHello_Click”, you’re pretty smart because you now know
what a “function” is. Anyway, this part is highlighted below in yellow:

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

namespace Helloworld
{
public partial class Dave : Form
{
public Dave()
{
InitializeComponent();
}

private void btnHello_Click(object sender, EventArgs e)


{

}
}
}
Now, observe the two “{“ and “}” curly braces holding the function together.
Currently, the function really doesn’t do anything because there is nothing in the
braces. However, when we code our code to do something for the “click” event
triggered by “btnHello”, you will see that the function will be very useful (especially
for the ‘pull my finger app’…that’s another matter). Anyway, when we want the
user clicking the button, I will make it say “I WAS CLICKED! AAAAGH”. However,
your button needs to be fairly bigger than the default size to fit that: The blue part
is the new code.

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

namespace Helloworld
{
public partial class Dave : Form
{
public Dave()
{
InitializeComponent();
}

private void btnHello_Click(object sender, EventArgs e)


{
btnHello.Text = "AAGH I WAS CLICKED! NOOOOO!";

}
}
}

Now, before you freak out, I only added one line and this is a fairly easy thing to
understand. Let’s dissect it piece by piece…
The devilish
Name of your semicolon
symbolizing the
btnHello.Text = "AAGH I WAS CLICKED! NOOOOO!";
end of the line.

A “string”, which is
The “dot” just text wrapped
symbolizes that the The name of your
property you want around in quotes.
“text” property is This can be
INSIDE of your to change. I
wouldn’t try other anything you want
button and that it is
a predefined properties until you
Now, you have probably still not yet understood the inner meaning with this line,
but let me tell you that I too was super confused when I learned about this, but it all
came together after looking at some code. Anyway, if you did it right, you can press
F5 and your program will execute. If you get some error, you will have to revisit the
code or doubleclick on the error and it will point to the exact line which has the
issue and it will most often give you a fix when you right click on the error.

*pic11*

Now, if you want to look at my code and see how you made a mistake, feel free do
download the sourcecode at:

http://www.mediafire.com/?ekky20dhgh5kx2s

You can download it there, unzip it and double click on the .SLN file. Then, visual
studio should be able to open it up and you can run it with no harm. If you have any
problems, you can post a comment on whatever forum or website this tute was
posted in and I’ll try to get back with details ASAP. Also, you can email me at
chinnambu@yahoo.com in case you really want to ask personally.

If you thought this tute was helpful, please donate to: chinnambu@yahoo.com via
paypal or simply click on the link below:

https://www.paypal.com/us/cgi-bin/webscr?
cmd=_flow&SESSION=JykBPD1ckSZB65lquXg6LY1SCVcthrGYmYlfKv-
gU4m3eXx3IlWf9CU__Z4&dispatch=5885d80a13c0db1f8e263663d3faee8d66edfb0
b39be7838e3b204755610594d

You might also like