Microsoft & .NET.NETInteracting with .NET WinForms, Part 1

Interacting with .NET WinForms, Part 1 content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

You can develop Graphical User Interface (GUI) applications based on the .NET Framework (just like applets in Java) by using Windows Forms or simply “speaking” WinForms. (With WinForms, though, you can develop only standalone GUI and client applications. If you want to develop applications for the Web, then WebForms should be used.)

The System.Windows.Forms namespace provides all the necessary methods, classes, and events for writing a complete GUI application. It contains large number of classes with which you can build powerful user interfaces for your applications.

Basic Requirements

First, you have to install the .NET SDK (Beta). It can be downloaded free of cost by clicking here. After downloading, double-click the setup.exe to run the installation. The Installation takes about 15 minutes, depending upon the speed of your computer. The SDK can be installed in Windows 98 and Windows 2000. But Microsoft recommends Windows 2000 for developing and implementing .NET solutions.
Windows 98 users should have to run a utility called corvars.bat (located in the bin directory) before compilation.

Notepad is widely used for writing source code. You can also use Visual Studio.NET for the same purpose. It comes with Visual C#. NET to simplify your programming tasks. You have to save your file with the extension . cs. Compile the source file by giving the command csc at the DOS prompt. Finally, execute your application by supplying the Filename.

Your First WinForm Program

Type in the following code using any editor and save the file as Hello.cs:

        using System; 
        using System.Windows.Forms;
        public class Hello:Form  {
        public Hello() {
        public static void Main()  {
        Application.Run(new Hello());

Execute the above program by using the command Hello. The above code produces a blank Form with Minimize, Maximize, and close buttons. The detailed explanation of the above program is given below for your reference.

  1. Lines 1 and 2 refer to the required namespace to be used in this program.
  2. Line 3 specifies the class Hello that inherits the Form class.
  3. Line 4 calls the class constructor.
  4. Line 5 specifies the main method, which is required in all programs. It is the entry point for every application.
  5. Finally, an instance of the class is created as an argument to the run() method of the Application class.

However, you can change the look and feel of the Form by applying various properties of the form class as described below. In the Hello() constructor, add the following code. The relevant explanations are shown as comments.

// Changes the forms location
this.Location = new System.Drawing.Point(300,300);

If you are using the System.Drawing namespace declaration, then the following code is permissible:
this.Location = new Point(300,300);

// Changes the forms Title
this.Text = "Welcome to Windows Forms"

// Changes the mouse cursor
this.Cursor = Cursors.Hand

// Changes the background color of the form
this.Backcolor = Color.Orange
You need to refer the System.Drawing namespace before attempting this code.

// Changes the forms start up position
this.Startposition = FormStartPosition.CenterScreen

// Changes the Forms client size
this.Clientsize = new Size(440,170)

// Changes the font name and color
this.Font = new Font("MS Sans Serif",10);

All the properties have to be accessed by making use of the respective Class or Class objects except for Form where the “this” keyword is used. “this” refers to the current instance of the form.

Generating a Message Box

Message boxes provide a convenient way to display user-friendly messages in a GUI environment. If you’ve used Visual Basic or Java, then you’ll be already familiar with the Message box. To add a Message box simply copy the following code to the constructor of the above program:

MessageBox.Show("This is a message", "First Box"); 

The Application class provides properties for retrieving the current application’s name, version, startup path, etc. It can be applied in the above program as specified below.

MessageBox.Show(Application.CompanyName, "Your Company Name");
MessageBox.Show(Application.ProductName, "Your Product Name");
MessageBox.Show(Application.StartupPath, "Your Path is ....");

Working with Events

When you click the close button on the form while executing the above program, you should have noticed that it would close on its own. It will be interesting if you activate some messages via Message Box while a user closes the form. For doing so, you have to handle the ApplicationExit event of the Application class as shown in the listing below:

using System;
using System.Windows.Forms;
        public class Hello:Form  {
        public Hello() {
                // invokes the ApplicationExit Event
        Application.ApplicationExit += new EventHandler(Form_OnExit);
              // Event Handler corresponding to the above invoking. 
        private void Form_OnExit(object sender, EventArgs e)  {
         public static void Main()  {
        Application.Run(new Hello());  

Form_OnExit as specified above is called an EventHandler. This handler must conform to a delegate of the class System.EventHandler. The first argument refers to the object sending the corresponding event and is of the class System.Object. The second parameter consists of information for the current event. You can use the object e to handle several functions, which we will examine in Part 2.


After successfully compiling the above program, take a look at the generated Intermediate Language or IL, by opening the corresponding .exe file from the ILDASM tool (ildasm.exe). ILDASM stands for Intermediate Language Disassembler. It shows all parts of your program in a tree view, which you can use for reference purposes. Keep in mind that contents of the file are un-editable.

About the Author

Anand Narayanaswamy is a graduate of the University of Kerala. He is currently working as an instructor teaching Java, Visual Basic, and technologies such as ASP and XML and a regular contributor to gamelan and sites. He enjoys learning new programming languages like C#. Currently, Anand lives in
Thiruvananthapuram, Kerala State, India. He can be contacted via his Website
or via email at

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories