Tutorial: C# Hello World

In this tutorial I will show you how to create the basic "Hello World" program using one form, a button and the messagebox class.  

1) Load up Visual Studio 2005 or Visual C# Express Edition.  
A. Click File/New Project
B. Select Windows Application
C. Type in the name of the project.  I used "Hellow World"

Posted Image



2) You should now have Form1 loaded into the IDE with panes to each side of the form.  The next thing we are going to do is add a button.  To add a button move your mouse over the toolbox which is a tab.  This should be on the right hand side as a little tab.  This location may be changed though.  Refer to the next image if you can not find the tab.

3) Once you see the new pane appear click on "button"

Posted Image


4) Move your mouse over to Form1 in the center of your screen.  Click and hold down the mouse button and drag the mouse to the desired dimensions of the button you want.

Posted Image

5) Now that we have our button created we need to add our method to make the "Hello World" message appear.  The easiest way to create this method is to simply double-click on the new button.  Once you have double-clicked on "button1" you should have code that looks like this:


 
private void button1_Click(object sender, EventArgs e)

               
{


               
}

5) In between the two brackets ( { and } ) add this code:


MessageBox.Show("Hello World");

Your code should look like this:

private void button1_Click(object sender, EventArgs e)

               
{

                       
MessageBox.Show("Hello World");

               
}

6) Press F5 to run your program.  Once the program appears on the screen press button1 and a messagebox should appear.

Posted Image

And that completes the tutorial.  From here you can start to play around and change things like the text of the button by right-clicking on the button and selecting properties.  A properties pane will appear on the right side of the IDE.  Find the Text Field and change it from button1 to anything you like.

No comments:

Post a Comment