Techblogg

C# .NET Absolute Beginners Guide




This little guide is for the absolute beginner.
I myself is also a beginner, however, the best way to evolve is to teach.

So, for the sake of simplicity, I´ll be assuming nothing about the reader,
except for a slight interest in programming and the ability to download and install applications.

The Setup

First things first, you need an IDE (integrated development environment) such as Visual Studio.
Visual Studio (We´ll be focusing on 2012 Express) is a fantastic tool and very much ahead of the competition.
It features a slick design, a compiler and can package your programs into neat little setup files for you.
Best part about it, it´s free!

Download here: Microsoft
You´ll need a Microsoft Live account when installing though.

Once you´ve downloaded and installed Visual Studio, start it up, and start a new project from the File menu.
Select Visual C# and Windows Forms, name the project something and press Ok!





Your first code

In front of you, you now have a "Form". I won´t be going into any detail about it, knowing that it´s what your program looks like will suffice for now.
On your left, you have access to a "toolbox", you might want to pin it using the little needle icon on top of it. Drag a "Button" from the Common Controls to your form.



Now, when your button is selected, you have access to a properties form on the bottom right.
There you can do all sorts of stuff, but since we´re learning C#, let´s stick to modifying the button using code, and not the properties form for now.

Double click your button. This will automatically take you to the code portion of the form, and it has created a new function for you.
The function is already assigned a "listener", in this case, it listens for the event of the user clicking the button.

As in, the function in front of you, dictates what´s going to happen when you click your button.

Try typing in: MessageBox.Show("I was clicked!"); between the brackets of your function.



Now, press F5 to run your program. If Visual Studio asks if you want to save, just do so. Try clicking your button. Pretty simple right?
Close the windows the top right cross and you´ll get back to Visual Studio again.

Creating a function
Ok, so let´s make the program do something more than just show a text box. Click the tab "Form1.cs [Design]"



Add a "listview" and a "Combobox" from the toolbox somewhere on your form, and go back to your code.

Now, make a new line directly below "InitializeComponent();" and type the following:

button1.Text = "Add Item"; // This sets the buttons text value
listView1.View = View.List; // This sets the listviews viewing type to List-mode

Now, add your own function called itemAdd by typing:
private void itemAdd(string Name)
{

}

It should look like this now:




What this means is that we´ve added a private function with the name itemAdd, that takes a parameter called Name.

Between the brackets, type in:

listview1.Items.Add(Name);
comboBox1.Text = "";


Good, now remove the line with the MessageBox.Show code and replace the line with the one below:
itemAdd(comboBox1.Text);

Now you´ve basically made a very simple input form that stores the value you type in a list. :)

Hope you learned something! I´ll post more advanced pieces of code later on.
Thanks for reading!