C# Tutorial 01 - Your First Window

(1/1)

Richard Marks:
Tutorial 01 - Your First Window

OK, welcome to the first tutorial in my Windows.Forms tutorial series. 8)
Today I am going to show you how easy it is to get started using Windows.Forms.

The program that we are going to create in each tutorial is going to be the base of the following tutorial.
However, the existing code will not be re-explained.
So, you should always read from the first tutorial so that you know what is going on.

I'm not going to repeat myself this time around, so if you didn't read this text (and if you didn't, then what are you doing NOW?!) then its not my problem when you can't figure things out!

OK, lets begin.

Wait..where do we begin? You don't even know what you need!
Right, so I am using Microsoft Visual C# 2008 Express, and I recommend that you do the same.

Once it is installed, run the Microsoft Visual C# 2008 Express IDE by clicking the nifty little icon it places on your Start Menu.

Oh yeah, and I am using Vista Home Premium 64-bit - you can use any Windows flavor you prefer as long as it is newer than XP.

So, you have Microsoft Visual C# 2008 Express running now, and you are staring at the shiny Green and White Start Page, yes?
Good.

Now, left click on the thing that says "Project..." next to "Create:", and a new window will open up.
Left click on Visual C# on the left panel, and then choose Empty Project and then type in a name for your project.
I am going to use YourFirstWindow as my project name.
Click OK.

Now, if you don't see something that says Solution Explorer then go to the View menu at the top, and pick Solution Explorer.

OK, now in the Solution Explorer, you should see your project and then something that says References.

Right-click on References, and then choose Add Reference... and a new window will open up.
From this window, scroll down until you see System.

Double click on the item that says System, and repeat the previous process to add references to these as well:

System.Drawing

System.Windows.Forms

Once you have done this, go save your project.
Click File at the top, then Save All.

If the defaults are good enough for you, then click the button labeled Save.
If not, then rename stuff and choose another location to save.
I recommend for the sake of simplicity to accept the defaults and save your project.

OK, now lets start writing some code. After all, that is why we are here!
Right-click on your project in the Solution Explorer (its above the References we added) and choose Add then New Item.

A new window will open up.

Choose Visual C# Items on the left panel and Class in the right, and give it the name Program.cs and click on the button labeled Add.

Now you should be staring at this in the main window:

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YourFirstWindow
{
    class Program
    {
    }
}


We are going to streamline this a bit, as it does not have just what we need it to have.

We will first remove the unnecessary using statements.

Edit the code so that it looks like the following:

Code:

using System;
namespace YourFirstWindow
{
    class Program
    {
    }
}


OK, now we need to make the class public, and write the static Main method that we need in all C# programs.
So, edit the code to look like this.

Code:

using System;
namespace YourFirstWindow
{
    public class Program
    {
        public static void Main()
        {
        }
    }
}


Now, save your project again. You should do this after every little change you make.
Saving is very important. Don't cry to me when your computer does something weird and you lose hours of hard work.
SAVE EARLY AND SAVE OFTEN!

OK, you should be able to run your program by pressing F5 - and nothing should happen.. YEP. That is right.
Its a valid C# program, that does NOTHING. Fun Eh? ::)

Lets get to the real fun, and the reason you are here.
I started this as if a complete newbie was reading it to make it more useful..
Anyway, if you already knew everything above, I'm sorry.
Read on!

Add another Class item to the project like we added Program.cs, this time name it PrimaryWindow.cs and here is the first way to edit the code:

Code:

using System;
using System.Drawing;
using System.Windows.Forms;
namespace YourFirstWindow
{
    public class PrimaryWindow
    {
    }
}


Now, we need to make our PrimaryWindow class inherit from the System.Windows.Forms.Form class.
This is easy, we just need to add a colon and the word Form to the code like so:
Code:

using System;
using System.Drawing;
using System.Windows.Forms;
namespace YourFirstWindow
{
    public class PrimaryWindow : Form
    {
    }
}


So far now we have almost what we need.
Save your project again.

Lets add a constructor to our class:

Code:

using System;
using System.Drawing;
using System.Windows.Forms;
namespace YourFirstWindow
{
    public class PrimaryWindow : Form
    {
        public PrimaryWindow()
        {
        }
    }
}


Save again. I'm not going to tell you again. After everything you do, SAVE. OK? That is the last time I'll remind you to SAVE!

Now, I'm not going to keep showing a full code dump with every little change.
I will just tell you where in relation to the existing code to place things.

In the constructor we just added, we are going to add some code to set the size of the window, and the window caption text.

Code:

Width = 800;
Height = 600;
Text = "Your First Window";


Now, open the Program.cs file if it is not already opened, and now we add the code that will make our window appear.

In the Main() method we will add the following code:
Code:

PrimaryWindow window = new PrimaryWindow();
System.Windows.Forms.Application.Run(window);


The first line creates the instance of our PrimaryWindow class, and the next line uses some Windows black-box magic to handle everything that used to be so damn difficult in writing windows programs for us. I'm loving this stuff! ;D

Two last things we will do before I call this tutorial finished.

First, lets change the project type from Console Application to Windows Application.
To do this, right-click your project in the Solution Explorer and click Properties.
A new page will open up.
You should see near the right side a drop-down box labeled "Output type:" and you need to simply choose Windows Application and then save the project so that your property changes are saved.

You can close the properties page by using the Close option from the File menu.

Lastly, I want to change the background color of our window.
Open PrimaryWindow.cs and add another line of code to the constructor:

Code:

BackColor = Color.Black;


OK, press F5 and you should see an 800x600 black background window appear. :o

Thanks for reading.
I'll post more later when I have time. 8)
Comments, suggestions, and questions are welcome. :)

Navigation

[0] Message Index