Need to host a subversion repository? Try our hosted development tools.

C# School

By Faraz Rasheed
Last Updated: Monday, November 03, 2008
Covers all you need to know to start programming in .NET and C#, from the basics to advanced topics.
starstarstarstar
                     Next Page



WinForms and Windows Applicatoins

If you are new to C# School
This is the 11th in the series of lessons of our C# School. The C# School is a kind of interactive learning platform where those who want to learn .NET with C# can find help and support. With one issue a week, describing some areas of the C# Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author ( farazrasheed@acm.org)or the WEBMASTER ( info@programmersheaven.com).

For previous lessons: Lesson Plan
Today we will start building Windows Applications in C#. We will start by looking at the architecture of Windows Application and their support in .Net. Later, we will design our first "Hello WinForm" Application and learn about various windows form controls. Finally, we will look at how Visual Studio.Net eases the creation of Windows Applications.

Windows Applications and .Net
C# and .Net provide extensive support for building Windows Applications. The most important point about windows applications is that they are 'event driven'. All windows applications present a graphical interface to their users and respond to user interaction. This graphical user interface is called a 'Windows Form', or 'WinForm' for short. A windows form may contain text labels, push buttons, text boxes, list boxes, images, menus and vast range of other controls. In fact, a WinForm is also a windows control just like a text box, label, etc. In .Net, all windows controls are represented by base class objects contained in the System.Windows.Forms namespace.

WinForm Basics
As stated earlier, .Net providesthe WinForm and other controls through base classes in the System.Windows.Forms namespace. The class System.Windows.Forms.Form is the base class of all WinForms in .Net. In order to design a windows application, we need to: 1.Create a Windows Application project in Visual Studio.Net, or add references to System.Windows.Forms and System.Drawing to your current project. If you are not using Visual Studio at all, use the /reference option of the command line compiler to add these assemblies. 2.Write a new class to represent the WinForm and derive it from the System.Windows.Forms.Form class:

	class MyForm : System.Windows.Form
	{
		...
	}


3.Instantiate various controls, set their appropriate properties and add these to MyForm's Controls collection. 4.Write another class containing the Main() method. In the Main() method, call the System.Application.Run() method, supplying it with an instance of MyForm.

	class Test
	{
		static void Main()
		{
			Application.Run(new MyForm());
		}
	}


The Application.Run() method registers your form as a windows application in the operating system so that it may receive event messages from the Windows Operating System.

Building the "Hello WinForm" Application
Let's build our first windows application, which we will call "Hello WinForm". The application will present a simple window with a "Hello WinForm" greeting at the center. The source code of the program is:

using System;
using System.Windows.Forms;
using System.Drawing;
namespace CSharpSchool
{
	class Test
	{
		static void Main()
		{
			Application.Run(new MyWindow());
		}
	}
	class MyWindow : Form
	{
		public MyWindow() : base()
		{
			this.Text = "My First Windows Application";
			this.Size = new Size(300, 300);
			Label lblGreeting = new Label();
			lblGreeting.Text = "Hello WinForm";
			lblGreeting.Location = new Point(100, 100);
			this.Controls.Add(lblGreeting);
		}
	}
}


Understanding the Code
At the start, we included three namespaces in our application:

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


The System namespace, as we stated in the first lesson, is the necessary ingredient of all C# applications. In fact, the Application class that we used later in the Main() method is defined in this namespace. The System.Windows.Forms namespaces contains the base classes for windows controls, e.g. Form, Label and Button. Finally, including the System.Drawing namespace is necessary as it contains the classes related to the drawing of controls. The Size and Point classes used later in the program are actually defined in the System.Drawing namespace.

Later, we derived a new class, 'MyWindow', from the Form class defined in System.Windows.Forms.

	class MyWindow : Form
	{
	...
	}


In the constructor of MyWindow, we specified the size and title of the form (by setting the size and text properties). The size is defined using the System.Drawing namespace's Size class. We passed two integers to the constructor of Size to specify the width and the height of the form.

	public MyWindow() : base()
	{
		this.Text = "My First Windows Application";
		this.Size = new Size(300, 300);


Next in the constructor, we created a text label and added it to the Controls collection of the Form. A text label is used to write some text on the form. The System.Windows.Forms.Label class defines a text label in a Windows application. We set the text of the Label using its Text property, which is of the string type. All the controls contained by a form must be added to its Controls collection; hence we have also added our label to this collection.

	public MyWindow() : base()
	{
		this.Text = "My First Windows Application";
		this.Size = new Size(300, 300);
		Label lblGreeting = new Label();
		lblGreeting.Text = "Hello WinForm";
		lblGreeting.Location = new Point(100, 100);
		this.Controls.Add(lblGreeting);
	}


Finally, we have created a Test class containing the Main() method. In the Main() method, we have instantiated the MyWindow class and passed its reference to the Application.Run() method so it may receive messages from the Windows Operating System.

When we execute the above code, the following screen is displayed:

http://www.programmersheaven.com/articles/faraz/lesson11_img1.gif
To close the application, press the close button on the title bar.


                     Next Page


Bookmark: Submit To Digg Submit To reddit Submit To del.icio.us Bookmark With StumbleUpon Bookmark With FaceBook Bookmark With Google Bookmarks
Comments 62 Comments (view all) Post Comment Post a Comment
Joules Castillon says: Student
Edge Posted on Friday, June 13, 2003Edgestarstarstarstarstar
Great master piece... I have never imagined of such programming like Turbo C#... I am expecting that it would be Turbo D... It's an excellent advancement in Computer Science
mkn says: Good Work
Edge Posted on Friday, June 13, 2003Edgestarstarstar
Great stuff for biginners and had a good look of language key elements. Hopefully, the next aritcle will be more interesting.
Asim Ali says: Keep writing !
Edge Posted on Friday, June 13, 2003Edgestarstarstarstarstar
It was really nice to know about the article and it was more nice to read .
says: 5 STAR
Edge Posted on Saturday, June 14, 2003Edgestarstarstarstarstar
Good effort. This will help us to improve our knowledge in the IT field thank u
Edge Posted on Saturday, June 14, 2003Edgestarstarstarstarstar
This text summarizes the .NET architecture, its features like CLR, CLS, CTR. It contains a good comparison between C++, C# and Java. Better than what many books have tried to do.
Edge Posted on Saturday, June 14, 2003Edgestarstarstarstarstar
No doubt the author, Faraz Rasheed , is a master mind. He is one of the best programmers in the university. The way he looks at Object Oriented techniques is really great, out of this world. I was surprised (and was proud too) when I saw him favoring C# in a discussion forum (Vs Java & C++) , which he won too :p Well Best of luck in future Faraz. We all are praying for you.
says: Good work
Edge Posted on Sunday, June 15, 2003Edgestarstarstarstarstar
This course is one of its type and has no parallel. Go ahead , Good Work!!!!!!!! Must continue to work on it with the same zeal:)
Sanjeev Ranjan says: A simple and nice code
Edge Posted on Tuesday, June 17, 2003Edgestarstarstarstarstar
Its a very simple and very easy written code. If any one is new then its good for them also. His explznztion is like a teaching lession in the class. I never seen better then this type of explanations.
Muhammad Adeel says: Great Work
Edge Posted on Tuesday, June 17, 2003Edgestarstarstarstarstar
Its a nice way and very helpful to those who really want to learn.
Syed Tahir Rasul says: wonderfully Awesome
Edge Posted on Wednesday, June 18, 2003Edgestarstarstarstarstar
Well, this is a great article to read at ProgrammersHeavan, one of the great assets on this site. Thank you Fraz
 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.