C# Tutorial - Part II (Writing Your Own Program)


  1. Introduction
  2. Part I - C# as a Language
  3. Part II - Writing Your First Program

Part II - Writing Your Own Program

Console vs Winform

In the last part you learned some basics about C#. Since a lot of the fundamentals that are required for programming are dry and boring, I'm going to jump ahead a little bit and just start coding. For most this is the most exciting part, putting code on the screen.

A lot of C# tutorials start out having you write a basic console application. It is true that these programs are very simple and to the programming minded person they are easy to understand. However I am of the opinion that these are unfamiliar and daunting to someone that has never had any programming experience. It is a little ironic but in the public consciousness a simple command prompt window is the sign of something tech heavy and only a tech savvy person would know how to use it.

In truth console applications are the simplest of windows applications, but in the world of today it's almost just as easy (if not easier) to create a basic windows application with buttons and windows like the one below.

This is the kind of program 99% of people are used to seeing when using the computer. Because of this it would seem to make sense to start from there, rather than confuse you with a console application with nothing you can relate to.

.NET Framework and Why it Helps

.NET, so you're not confused, is simply the name given to Microsoft's subset of core programming technologies. Don't ask me why they decided to use such a confusing ambiguous name. The .NET Framework however, is exactly what it says; it is a framework. This framework contains libraries of pre-written code that are available to you as the programmer. This pre-written code is what makes the .NET Framework so powerful. Everything in .NET is built around the framework.

Because I want this tutorial to be simple I won't bog you down with a bunch of technical bits about the .NET Framework. However, if you are at all confused about what C#, .NET Framework, or Visual Studio are, then please see this post I made recently that explains it a little better.

Winforms or WPF?

Today there are a lot of different technologies that allow you to quickly dish out a windows app with forms and buttons, but none as hardy as Winforms. Winforms is simply the name given to the library of .NET code that allows you to build windows forms applications right out of the box. There is a new technology called Windows Presentation Foundation (WPF).

WPF makes it easier to create flashier applications and also separates the user interface from the business logic much better than winforms does out of the box. However, WPF is still new and there is not nearly as much support out there for it as there is for Winforms. Winforms is still a very valid/viable technology and a great starting point for beginners. After learning Winforms, WPF will be a cinch to adapt to.

Alright, can I code now?

Okay okay, geez, calm down. Now that we've decided to go with Winforms for our first program, let's get started. First you will have to create a new windows application project. The instructions I provide will be specific to Visual Studio 2010, but it shouldn't take much work to find the discrepancies between that and the express edition.

  1. Open up Visual Studio and go to File > New > Project.
  2. Choose "Windows" under "Visual C#" on the left hand side. Then select "Windows Forms Application" as your project type.
  3. For the sake of simplicity, let's name it "HelloWorld" and click OK.

You should now see a project open up. In it you will see a designer with, you guessed it, a windows form. Go ahead and press F5 to run your project. Notice that it already runs and a huge amount of work is done for you. Because of the winforms libraries you already have a working windows form and you didn't write any code at all! Notice on the left hand side there is a box titled "Toolbox". The toolbox is where you will find a list of controls available to you as you program. Watch how simple this is.

Expand the node in toolbox called "Common Controls". You should see a list of controls that probably look familiar to you.

Let's drag a "Button" onto your form. Simply click and hold down the mouse over the button control and drag it over the top of the winform.

Now double click the button. A snippet of C# code will be generated for you that should look like this:

private void button1_Click(object sender, EventArgs e)


This is what is known as an Event Handler. An event handler is a method that is called when an event is fired. In this case you are handling the "click" event. Any code you place inside this snippet will be executed when the user clicks the button. Double-clicking controls you place on the form will generate a snippet for that control's default event. In the case of the button control, the default event is "click".

In this snippet lets write a quick line of code so we can see it in action:

private void button1_Click(object sender, EventArgs e)
	MessageBox.Show("Hello .NET World!");

After modifying your event handler to look like the one above, press F5. Your form will pop up with your button on it. Click the button.

You should have seen a message box pop up like the one above. What just happened? Well you generated an event handler for your button that handled the button's click event. When you clicked the button you triggered the code in your event handler to run. That code called an existing .NET library that knows how to generate message boxes. See how easy that was? You just finished your first windows application, with buttons and all!

If you are like me, then this is the point when you put down the book and start dropping tons of controls on your form and playing with them all. Just be careful, you might burn your F5 key off the keyboard through overuse :P.

The Properties Window

One area of visual studio you need to pay close attention to is the properties window. The properties window is located in the bottom right of visual studio by default. Do you remember in Part I of this tutorial when we built a custom TV object and then set properties on it that described the object? Well this is what the properties window is for. All the controls you are dragging onto your form are objects in the code. Just as you would expect, these objects have properties, a lot of properties.

You should have two tabs open in the middle of your visual studio window: Form1.cs and Form1.cs [Design]. Go back to the design tab to view the designer again.

Click on your button control. Notice the properties window in the bottom right corner.

It should show that you have your button selected and give you a list of properties to set. Pay special attention to the "(Name)" property. Note that this isn't actually a property of your button object. It is the name of your button object. For example:

TV sonyTV = new TV();

sonyTV was the name of our TV object. You always want to ensure your names are meaningful and that they describe the object they represent very well.

Change the name of your button to something like "btnExampleButton". A form full of controls called "button1", "button2", etc is hardly readable. This name is how you refer to your controls in the code.

To demonstrate how you reference controls from in your code, let's drop a label on your form. Find the label control in the toolbox and drag it over.

Change the name of your label to "lblExample".

Locate the Text property on your label control and change it to say "No button clicked yet :(".

Next locate the Text property on your button and change it to say "Click Me!".

Now double-click your button again. If this is the same button we created before then it should already have an event handler. If it's a new button then it will create a new event handler for you. Modify your event handler to look like this:

private void button1_Click(object sender, EventArgs e)
	lblExample.Text = "You clicked your example button! Good job!";

Notice how similar that line of code looks to the lines when we were setting properties on our TV object in the first part of this tutorial. The designer did all the work of creating the label object for you, now you are just customizing some of its properties. lblExample.Text refers to the Text property of your label object called "lblExample".

  1. Press F5 to run your program. Click your button and see what happens!

You should now have a decent idea of how your controls can interact with each other. Just keep in mind when you are coding that almost everything in an object-oriented language is an object. There are objects created with one or two properties and there are objects created with hundreds of properties. Also keep in mind that an object could have a property containing another object. For example:

// Define a Person object.
public class Person
    public string Name { get; set; }
    public int Age { get; set; }

// Define a Car object. Notice that it has a property of type Person.
public class Car
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }
    public Person Owner { get; set; }

Now create your objects somewhere:

Person myPerson = new Person();
myPerson.Name = "John Doe";
myPerson.Age = 24;

Car myCar = new Car();
myCar.Make = "Nissan";
myCar.Model = "350Z";
myCar.Year = 2011;
myCar.Owner = myPerson;

Then later in your code say that you wanted to know the name of the owner of the car:

string ownerName = myCar.Owner.Name;

Well that is pretty much it for this tutorial. I hope you learned enough to get the ball rolling!

What You Just Learned

  • The basic differences between windows and console applications.
  • The fundamental differences between Winforms and WPF.
  • How to start your own Winforms project in Visual Studio.
  • How to start using your own controls and how they relate to the object-oriented code samples from Part I
  • How objects can contain other objects as properties, not just simple variables.
  • That objects are still the basis for life, the universe, and everything.

<< Part I