AddThis Social Bookmark Button

Print

A Template for C# Windows Applications

by Budi Kurniawan
05/13/2002

A Windows application normally comes with some standard features: controls, menus, one or more toolbars, and a status bar with one or more panels. Every time you start to write a new Windows application, you often start with the same ritual: creating the above components and adding event handlers for them. You can save several hours of work if you work from a template that already defines these standard components.

This article offers such a template, and also also provides some essential knowledge of C# Windows programming with the Microsoft .NET Framework class library. The template presented here is especially useful if you're not using an IDE like Visual Studio .NET. If you are, you can still get some benefits by learning how everything works beneath it. The template itself is given in Listing 1 at the end of the article.

When writing a Windows application, here are the steps you normally take as preparation, before you start coding your business logic.

  1. Create a form.
  2. Add controls to the form.
  3. Add a menu and menu items and bind the menu to the form.
  4. Create a toolbar.
  5. Add a status bar.
  6. Add event handlers.

With the template provided in this project, however, you can skip these steps entirely -- you just need to make some modifications here and there. The "raw" Windows application provided by this template is shown in Figure 1.


Figure 1. The Windows application template.

Creating A Form

In .NET Framework programming, a form is always a child of the System.Windows.Forms.Form class. Inheriting this class, without even adding an additional line of code, is sufficient to create a blank window. Therefore, if your class is named MyWinApp, here is the class definition in C# that inherits the Form class. Notice that you use a colon to specify inheritance.


using System.Windows.Forms;

public class MyWinApp: Form {

}

Like any other application, you need a static Main method as an entry point to your program. In the Main method, you can use the Run method of the System.Windows.Forms.Application class to start your application and show the form. The Run method has three overloads. For a Windows application, you use the overload that accepts a Form object.


public static void Main() {
    MyWinApp form = new MyWinApp();
    Application.Run(form);
  }

Alternatively, you can write the two lines of code in the Main method in one single line.


public static void Main() {
  Application.Run(new MyWinApp());
}

Setting Properties

Once you have a form, you may want to set its properties (such as width, height and title) and assign it a custom icon like the one shown in Figure 1. You can place the code that does this in your constructor, or in another method that is called from the constructor. In the template given in Listing 1, there is a method called InitializeComponent that is called from the class's constructor. You write initialization code in this InitializeComponent method.

For its dimension, specify the number of pixels for the Form class's Width and Height properties:


    this.Width = 400;
    this.Height = 300;

For the title, you use the Text property of the Form class.


this.Text = "My Windows Application";

Setting the Icon

If you don't specify one, a Windows application will show a default icon. You can use your own icon by setting the form's Icon property. This property accepts a System.Drawing.Icon object. The Icon class has several constructors you can use to instantiate an Icon object. In the template given in Example 1, I use the constructor that accepts the path to an .ico file, passing the complete path to an icon file named applicationLogo.ico.


    this.Icon = new Icon(imageFolder + "applicationLogo.ico");

where imageFolder is a static field that specifies the directory of the icon file. Note that imageFolder is defined as follows:


static String imageFolder = "Images" + Path.DirectorySeparatorChar.ToString();

This tells the application that the icon file is to be found in the Images directory, under the application directory. I used the System.IO.Path class's DirectorySeparatorChar property to obtain the character the operating system uses to separate a directory from its parent. I did not hard-code it by using a backslash, which is the valid directory separator character for Windows operating systems, in order to make sure that the template is portable to other operating systems, such as Linux and Unix. Yes, if you're wondering, .NET Framework has been ported to Linux, in some projects such as Ximian's Mono and Portable .NET.

Positioning the Form

It's useful to center your form. For this, you should use the StartPosition property and assign it one of the members of the FormStartPosition enumeration. To center your form, use the following code:


this.StartPosition = FormStartPosition.CenterScreen;

Note that you can also center your form by using the CenterToScreen method of the Form class; however, this method is not intended to be used directly from your code.


this.CenterToScreen();

The Form class also has some other interesting properties and methods. Here are some of the things you can do:

  • Create a transparent or semi-opaque form by setting its Opacity property.
  • Make your form modal or modeless by using the Modal property.
  • Change the form's background color with the BackColor property.
  • Make your form the top-most object in the application by setting the TopMost property to true.

Pages: 1, 2, 3, 4

Next Pagearrow