Windows Forms in C#

You use the Windows Forms section of the .NET Framework to write rich-client applications (otherwise known as Windows applications).

This chapter covers the initial steps of developing an application in Windows Forms. You’ll get the chance to expand the application in later chapters.

1. Creating Your Application

To best understand how to write Windows Forms applications, it’s useful to develop a real application.

Recently, one of our test machines was running out of space on its C: drive. To try to find out what was happening, we spent some time looking in likely directories, but it wasn’t an easy task. What we really wanted was a way to see what had changed so we’d know exactly where the disk space went.

The solution is an application named DiskDiff. It will show a tree view of the space used on a system, and it can compare the space used at two different time periods.

2. Getting Started

The first task is to create a default Windows Forms application. You do this by creating a new C# project and choosing Windows Application as the template. The name is DiskDiff, as shown in Figure 35-1.

The VS .NET environment will create the initial project and show the initial form. In Windows Forms projects, a form is simply a class derived from System.Windows.Forms.Form. You’ll see these forms listed under a project in the Solution Explorer with a special form icon, as shown in Figure 35-2.

You can view a form class in two ways. You can view the code of the form by right-clicking the form and choosing View Code. By double-clicking the form (or by right-clicking and choosing View Designer), you can view the form in the Form Designer (more about that in a bit).

The Windows Forms architecture differs from other Microsoft approaches in that the layout and controls for a form are implemented in the code of the form class, rather than in some sort of resource storage.

The initial class for this project looks like this (with the XML comments removed for clarity):

namespace DiskDiff {

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

public class Form1 : System.Windows.Forms.Form {

private System.ComponentModel.Container components;

public Form1()

{

//

// Required for Windows Forms Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

public override void Dispose()

{

base.Dispose();

components.Dispose();

}

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.Size = new System.Drawing.Size(300,300);

this.Text = “Form1”;

}

public static void Main(string[] args)

{

Application.Run(new Form1());

}

}

}

This class is fairly simple. The constructor for the class calls InitializeComponent(), which is the member that the class designer will use. Initially, all it has is a container for the other items on the form and lines to set the size and text of the form. A Dispose() member will clean up the form when it closes.

Finally, the Main() function for the whole application creates an instance of the form class and then calls Application.Run(), passing the form. This will execute the standard Windows message loop to get messages and dispatch them to the appropriate objects.

3. Using the Form Designer

The VS .NET environment makes laying out forms easy. You can view the Toolbox by clicking the Toolbox tab along the left side of the development environment. Selecting Windows Forms will show all the controls you can place on a form.

The directories you want to show make the TreeView a good choice, so drag one onto your form. The designer will show you the TreeView object and add the following code to the InitializeComponent() method:

this.treeView1 = new System.Windows.Forms.TreeView ();

treeView1.Location = new System.Drawing.Point (200, 112);

treeView1.Size = new System.Drawing.Size (121, 97);

treeView1.TabIndex = 0;

You’ll want the TreeView control to take up all the space in the form. You can do this by resizing the control when the size of the form changes, but the Windows Forms architecture can do this for you. If the TreeView object is selected, you can set the Anchor property in the property window. Initially, the object is anchored to the top-left corner; by setting the anchor to all four sides, you can resize the tree view whenever the form is resized. We’ve used anchoring in this case, but since this form has only a single object, you could achieve the same effect by setting the Dock property on the TreeView to DockStyle.Fill.

Now that the basic framework of the application is ready, it’s time to move onto some disk operations.

Source: Gunnerson Eric, Wienholt Nick (2005), A Programmer’s Introduction to C# 2.0, Apress; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *