Introduction to Windows Forms

The Hello World Application

Listing 3.1.1 is a simple Windows Forms application that displays the text "Hello Windows Forms!" in a label on the main form.

Listing 3.1.1 HWF.cs: The Hello Windows Forms Application

// HWF.cs
namespace HelloWindowsFormsNamespace {

  using System;
  using System.Drawing;
  using System.ComponentModel;
  using System.WinForms;

public class HelloWindowsForms : System.WinForms.Form
{
  //Label 1 displays the text message "Hello Windows Forms!"
  private System.WinForms.Label label1;

  //The constructor is where all initialization happens.
  //Forms created with the designer have an InitializeComponent() method.
  public HelloWindowsForms()
  {

    this.label1 = new System.WinForms.Label();

    label1.Location = new System.Drawing.Point(8, 8);
    label1.Text = "Hello Windows Forms!";
    label1.Size = new System.Drawing.Size(408, 48);
    label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24f);
    label1.TabIndex = 0;
    label1.TextAlign = System.WinForms.HorizontalAlignment.Center;

    this.Text = "Hello World";
    this.MaximizeBox = false;
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.BorderStyle = System.WinForms.FormBorderStyle.FixedDialog;
    this.MinimizeBox = false;
    this.ClientSize = new System.Drawing.Size(426, 55);
    this.Controls.Add(label1);
  }

  // This main function instantiates a new form and runs it.
  public static void Main(string[] args)
  {
      Application.Run(new HelloWindowsForms());
  }
}

} // end of namespace

You can see that the class HelloWindowsForms contains a simple label component, label1. It has only two methods: a static Main method that creates and runs an instance of the HelloWindowsForms class and a constructor in which all of the component initialization occurs.

This shows the minimum functionality and minimum complication of the Windows Forms system. Programs designed and maintained by VS.NET or WinDes.exe have some added functions and data members that detract from the simplicity of this example, but are necessary for the RAD environments to keep track of the form design.

To build the executable, we have prepared a small batch file that creates a Windows executable and references all the correct library DLLs. Using this batch file will allow you to stay away from the Visual Studio build environment for a while—because it hides too much of the important information—and concentrate on the actual functionality. Listing 3.1.2 shows build.bat.

Listing 3.1.2 build.bat

csc /t:winexe /r:Microsoft.win32.Interop.dll, system.dll,
         system.configuration.dll, system.data.dll,
         system.diagnostics.dll, system.drawing.dll,
         system.winforms.dll /out:%1.exe %1.cs
         %2 %3 %4 %5 %6 %7 %8 %9

build.bat is available with the software that accompanies this book, but if you want to type it in yourself, make sure that it's all on the same line and there are no spaces between the libraries named in the /r: reference directive. To use build.bat, simply type build followed by the name of the .cs file to compile. For example

build HWF

will compile the HWF.cs file and create HWF.exe.

Running the hwf.exe program will produce a form shown in Figure 3.1.1.

Figure 3.1.1
The Hello Windows Forms Application.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond