Blog Archive

Friday, May 7, 2010

Creating Windows Form Through C#

If you are new to C# please read My Older post 'Hello World in C#' before reading this post.

So you learned how to create and compile a simple C# program on command line. Now lets get hands dirty in some GUI programing through C#.
In this tutorial we will create a windows form and place button on it.
Before starting GUI prgramming you should familier with some terminology of C#.
like import statement in java, include statement in c and c++, C# has keyword 'using' to import system libraries. For this example we need to import
System.Windows.Forms in our program. Like packages in java and header files in c and namespace in c++, in C# there is namespaces which contain classes. There are many predefined namespaces provided in C# for different purposes. Like library files in these name spaces are stored in dll files. If want to use a namespase which is other then predefined namespases, you need to add refernce to dll files containing thoes namespaces.
we can create our own namespace by using 'namespace' keyword.
When you will run following example fllowing window will see following window on the screen.

Now lets see the code.

using System.Windows.Forms;
using System.Drawing;

public class MainClass
public static void Main()

//Creating instance of form class provided in System.Windows.Forms
Form form = new Form();

//creating instance of Button class provided in System.Windows.Forms
Button button = new Button();
//creating instance of Point class provided in System.Drawing
Point p1 = new Point(10,10);
//setting button's location on screen
button.Location = p1;
//this text will show on the top of button
button.Text ="Press Me";
//adding button to form

//creating instance of Label class provided in System.Windows.Forms
Label label=new Label();
//creating instance of Point class provided in System.Drawing
Point p2 = new Point(20,40);
//setting locaton of label
label.Location = p2;
//creating instance of Size class provided in System.Drawing
Size size = new Size(250,50);
//setting size of label on the screen.
label.Size = size;
//this text will appear on the place of label.
label.Text = "Rahul's Programming Heaven";
//adding label to form

//displaing form on the screen.

To compile above example you need microsoft visual studio installed on your system.
You can download express edition on following link:
After installing visual studio or c# express edition you are ready to compile your first c# program.

To Compile Above Program Do Following:
Go to > All Programs > Microsoft Visual Studio OR Visual C# Express> Visual Studio Tools > Visual Studio Command Prompt.

Now a Console window will appear.
1) Type the following command to the console window just opened, and press enter.
edit window.cs

2)Now a dos editor will appear on console, window type above code in editor. After then save and exit from editor.

3)To compile helloworld.cs just created, type following command, and press enter.
csc window.cs

Following output will appear on screen(Output can vary for different versions of visual studio).

Microsoft (R) Visual C# 2010 Compiler version 4.0.20506.1
Copyright (C) Microsoft Corporation. All rights reserved.

4)To run our program type following and press enter.

No comments:

Post a Comment