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
Faraz Rasheed is a student of BSCS in the department of Computer Science at the University of Karachi, Pakistan. He is also part of Operation Badar - an IT educational movement in Pakistan, and an international student member of the ACM (Association for Computing Machinery). He has a strong interest in Object Oriented Analysis and Design (OOAD) and development using programming languages such as C#, Java, VC++ and VB.Net. He can be contacted via farazrasheed@acm.org or frazrasheed@hotmail.com.
Related articles
None found.