C Sharp Development 101 – Part 1: Hello World

In this tutorial we are going to get up close with the Visual Studio 2010 environment. We will learn how to make a console application as well as a form to display our hello world applications.  This will give us an opportunity to view 2 types of solutions of the many available in Visual Studio.  We will start making the console application first then progress to the forms application.

First we must  understand the development environment we are going to use.  On the far left side is the toolbox panel.  This panel gives us access to a lot of controls  that can be used by the Windows Forms.  Next is the Solution Explorer that will allow us to navigate the projects and files we are going to create in this Solution.  The Properties panel is directly under my Solution Explorer and will allow us to change properties of controls and of the form we will create later on.  If any of these are not being displayed they can be retrieved from the View menu at the top under Other Windows.  For more information on the Visual Studio IDE visit MSDN and search for the specific questions you are having.

Then we need to start the Visual Studio environment and create a new project.  To do this we will go to File then navigate to New Project and click it.  A dialog box will appear and ask you which project you would like to include in your solution that will be automatically created for your project.  We need to use the Console Application.  Next we need to replace the box at the bottom where it says ConsoleApplication1 with HelloWorldConsole and then after the project and solution is created press CTRL-S to change the name of the solution file to HelloWorld in the box under the project name box and press OK.  This will create a project inside a Solution file. The solution file acts like the glue that binds all projects included in the solution file together.  Later on we will discover how this is beneficial for creating projects and making class files that reference DLL’s that we will code.

Once the project is created we are going to edit the program.cs file.  After you have open the program.cs we are going to add the text necessary to have the program output “Hello World” to the console.  To do this we will need to add the line Console.Out.WriteLine(“Hello World!”); inside the static void main curly brackets.  After this is complete we can now build and attempt to build our solution.  To build the solution we need to press CTRL + SHIFT + B and the build process will being.  After the build is a success we can now run the Console Application by pressing CTRL + F5. This will display a command prompt with “Hello World!”.

Here is the source code for program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorldConsole
{
class Program
{
static void Main(string[] args)
{
Console.Out.WriteLine("Hello World!");
}
}
}

We can now move on to the windows forms application of Hello World.  To do this, we need to go to the solution and Right Click, then go to Add then click New Project.  For the project we will name it HelloWorldForms.  After the project is created we are going to delete the Form1.cs and we are going to create a new form by Right Clicking the HelloWorldForms Project, Navigating to Add then to New Item,  and when the dialog box appears we are going to pick Windows Form.

The name we are going to use is main.cs and press Add.  We now edit the program.cs to change the Form1 that can be found in the file to main.  After the Windows Form is created we can start adding in Controls from the Toolbox.
We are going to drag a label and a button onto the form portrayed in the middle of the program.  We are going to edit the properties here to make the text inside the label blank and the name of the label lbHelloWorld instead of label1.  After this is done we are going to want to edit the button we dropped onto the form earlier.  We will change the name of the button to btnHelloWorld and the Text of the button to Click Me!.  After this is done we are going to want to use an event handler to tie the button and the label together, so when the button is clicked “Hello World!” will appear in the label.

To make an event handler for the button we are going to go to the Properties panel and click the button on the top that looks like a lightning bolt.  This will take us to all o the event handlers that this button can handle.  We want the Click event handler, this will create the code required to handle a click event in the main.cs.  Now that the wrapper is there we can code the output to the label when the button is clicked.  Inside the curly brackets of “private void btnHelloWorld_Click” in main.cs input the following line of code to link the two Controls:


lbHelloWorld.Text = “Hello World!”;

This will make the main.cs look like this:

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 HelloWorldForms
{
 public partial class main : Form
 {
 public main()
 {
 InitializeComponent();
 }

 private void btnHelloWorld_Click(object sender, EventArgs e)
 {
 lbHelloWorld.Text = "Hello World!";
 }
 }
}

The program.cs should look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HelloWorldForms
{
 static class Program
 {
 /// <summary>
 /// The main entry point for the application.
 /// </summary>
 [STAThread]
 static void Main()
 {
 Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new main());
 }
 }
}

After all of this is completed we need to run the program by pressing CTRL + F5 again. The screen that should appear should be something like this:

The screen after the button has been pressed should look like this:

Now that we have completed the next tutorial you should be able to move through the Visual Studio IDE to make multiple projects under one solution, delete files within a project and create new forms and classes, and modify source code within event handlers.  The next tutorial will go more in depth with the Visual Studio Toolbox and make a form with controls on it with minimal backbone code, as well as review some of the common files created and what is automatically included for you.  For more information on Toolbox Controls you can check out Microsoft MSDN article on Toolbox Controls. If you are having any trouble with this project feel free to comment and I will help to try and resolve the issue.  Until next tutorial, Happy Hacking!

Comments

  1. Ugly American says:

    What would your reaction be if you saw a public school teaching kids Esperanto instead of English, Mandarin or Spanish?

    Is Windows 7 written in C#? No. Word? No. Excel? No. Access? No. MS’s own 360 games? No. Their web pages? No.

    Why would you push people to invest time in learning a product that the vendor doesn’t even use themselves?

  2. Rollyn01 says:

    @Tachikoma

    Sorry, I misunderstood your comment. I see what you’re talking bout… and actually wonder myself why they wouldn’t. Seems like a nice idea, but then, Microsoft is always full of “nice” ideas.

  3. Yimmy says:

    Has anyone even tried to follow these? They flow like a drunk oompa loompa wrote them. Follow it and there are MAJOR gaps….nice try HAD, next time have a second party try to follow along before publishing. Looking forward to future installments.

  4. RvnKnight says:

    *stepping on soapbox*
    Stop whining about what the language is or is not, about why it was posted, and about what you think hacking is versus what it has been defined as. Hacking is about LEARNING SOMETHING NEW. Almost anything you can do in C/C++, someone else can do in another language or even in hardware. Learning to use what you know to modify, gain access into the inner workings of, or just figure out something is a form of hacking–it’s not limited to just chips and motors. For those who ask why M$ designed C#, look through the history of the language. Yimmy, since you wanna complain about gaps in the tutorial, why not write a new one on C#? Hell, you may even see one by me on Assembly (NASM) or even hacking a bootloader for you OS one of these days.

    *steps off soapbox*
    Now who wants pie?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 92,391 other followers