We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Book Cover C# and the .NET Framework
61324 times
Rated
Read 61,324 times

Contents

Related Categories

Introduction to Windows Forms - The Hello World Application

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.

Comments

  • short-cuts

    Posted by rholden on 15 Jul 2005

    this is not a programming issue and it is not something you can set unless you have a custom menu control.

    if you want to view the _ all the time for alt shortcuts you have to go to:

    display pro...

  • Sort-cuts

    Posted by DotDot on 15 May 2003

    The "_" on the short-cut character is visible only when "ALT" is pressed. Is it possible to display the "_" programatically???:confused: