FANDOM


The Hello World program will be a simple program to display a message to the user saying hello world. There will be several variations on this program to display the variety of available methods of user interface design.

Prerequisites Edit

Assumed knowledge Edit

  • An ability to read and use a keyboard/mouse.

Required programmes Edit

Instructions for a Console ApplicationEdit

Create a console application Edit

  1. Open your chosen integrated design environment (either visual C# 2008 express edition or Visual studios 2008 Professional edition)
  2. Select file->new->Project to create a new project.
  3. In project types select Visual C#
  4. Select Console Application as the template.
  5. Make sure there is a tick next to create directory for solution.
  6. Put HelloWorld as the solution name
  7. Leave the default name of ConsoleApplication1 as it is fine for our demonstration purposes.
  8. Click OK to create the project

Program a console application Edit

At this point you should see a window with Program.cs in it the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
}

The Curly brackets "{}" beloe static void main denote the start and finish of the code that will be executed when the application is run.

To begin, enter the following lines of code in this space.

Console.WriteLine("Hello World");
Console.ReadLine();

The ; denotes the end of each line.

The readline is needed because otherwise it would close too quickly for you to read the message.

Test a console application Edit

On a toolbar near the top of the screen roughly in the middle is a play icon.

  1. Click the play icon to build and run the application. (alternatively press F5 as this does the same thing)
  2. If all the instructions have been followed correctly you should now see a black box looking a lot like a command prompt window with Hello World written at the top.
  3. Press enter to close the application

Taking it furtherEdit

  1. Open up your existing console app from above.
  2. Replace the existing Console.WriteLine and Console.ReadLine lines with:
Console.WriteLine("Type your name here:");
String Name = Console.ReadLine();
Console.WriteLine("Hello " + Name);
Console.ReadLine();

3. Test the application by pressing run (or F5), type your name and press enter. It should say hellow followed by your name.

String refers to a string of text and is a named variable. Consider it like a box in which we can store information.

We make it equal to the information read off of a line on the console (this should be the user's name)

We then use this variable when we write the line onto the console.

The operator +, when applied to two strings will add them end to end (concatinate them).

Instructions for a Windows Forms AppEdit

Create a Windows Forms Application Edit

  1. Open your chosen integrated design environment (either visual C# 2008 express edition or Visual studios 2008 Professional edition)
  2. Select file->new->Project to create a new project.
  3. In project types select Visual C#
  4. Select Windows Forms Application as the template.
  5. Choose Add to solution instead of Create new solution (this allows us to put all our hello world programs in one solution.
  6. Click OK to create the project
  7. Since it has been added to an existing solution you should now find the solution explorer (if it is not visible the re is a symbol for it with a folder, window and magnifine glass near the top right of the window)
  8. In the solution exploer, right click on WindowsFormsApplication1 and select Set as startup project.

Program a Windows Forms Application Edit

At this point you should see a form that looks just like a window you would expect to see in windows with a Form1 written in the title bar.

Double click on it.

This should now look much like what you saw in the console application. In this case our code goes in the form load event. I.e. it goes in here:

private void Form1_Load(object sender, EventArgs e)
{
}

Although there apears to be a console, commands like Console.WriteLine won't actually do anything.

Instead put the command:

MessageBox.Show("Hello World");

in the form load.

This code is run every time the program starts, just before the form is shown to the user.

Test it by clicking run to see it in action.

Add a Button Edit

Click the tab at the top of the screen labled "Form1.cs [Design]"

Lock for a window labled toolbox. If you can't find one click the hamer and wrench symbo in the top right of the screen to show the toolbox.

From the toolbox drag a button into the centre of your form.

Double Click on the button.

You should then see the following code:

private void button1_Click(object sender, EventArgs e)
{
}

This code is executed whenever someone clicks on that button.

Add this code between the curly brackets

MessageBox.Show("Hello World");

Test the applicatioin and click on the button.

Add a Text box Edit

Add a text box to the form in the same way as you added a button.

Return to where you put the code for the button event and replace the code we put there before with:

MessageBox.Show("Hello " + textBox1.Text);

Test this applicatioin by typing your name in the text box then clicking on the button.