Subscribe

Your First C# Application

Posted by Administrator on Thursday, June 30, 2011

We are now going to create a simple application as your first C# programme. Before building this application you should already install Microsoft .Net Framework and Microsoft Visual Studio 2008 as we discussed earlier.

And the other thing is you should have a basic idea of


So Let's start our first C# application. Here what we do is, We create a simple user interface with a button. When the button is clicked, the message "Hello World!" is raised.

Step 1: Start Visual Studio
Open the Microsoft Visual Studio 2008. And It is look like this when you start visual studio first time.


Step 2: Create a new project
Then Using visual studio, we should create a new project.
Go to File -> New Project...  or Press Ctrl +Shift + N
Then New Project Dialog Appears.



In the New Project Window, Select Visual C# as Project type and Windows Forms Applications as the template. Give Name and Location to your project and finally click OK button to create our first C# project.

Step 3: Design the user interface.
When the project is created, You will see the designer view of your interface as follows.

Form Designer View

In this designer view of the form (Form1.cs [Design]), you can design the user interface of the single form. To do that, We use the 'Toolbox' which contains the items that you can add to your form. Toolbox is placed on the left side of your visual studio. If it is not visible Go to View -> Toolbox to show the Toolbox.   




Toolbox


It contains Lables, Buttons, Check Boxes, Combo Boxes and etc.. which can be used to design your interface. To add elements from the Toolbox to your form double click the item or drag the item to your form.
Now add a Button Control to your form by simple dragging a Button control into the form designer view. Finally it looks like below.



Now our form contains two elements. Those are form and the button control.  These elements have properties such as name, text, background color, fore color, etc.... To see properties for a control, select the control and all the properties are displayed in the Properties Window which appears on the right side of your visual studio. If it is not visible, Go to View ->  Properties Window. 

Select the button and view properties as follows.


Now set the text to 'Show' for the button. Then select the form set text to 'Hello World' from the Properties Window. Then the form should look like below.


Step 4: Writing the code
When we click the button, a message with 'Hello World!' should be shown. To do that we have to go to the code view of the form. Double click the button and visual studio automatically switch to the code view (Form1.cs). In the button1_Click event (this will execute when we click the button), write the code to raise the message box with  'Hello World!'.

Code to show the message box:

MessageBox.Show("Hello World!");


Now the form1.cs looks like below.


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

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

Step 5: Compile the code
To compile the code Go to Build -> Build HelloWorld

Step 6: Running the application
To run/execute the programme  press F5.


Running Application.

So its over. Your first C# application. Try more tutorials to become a C# expert. 

0 comments:

Subscribe to: Post Comments (Atom)