Library sample chapters

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.

Comments

  1. 13 Aug 2008 at 19:50

    Actually all you have to do in place an ampersand before the letter you want underlined.

    So if you want the "r" in "Print" underlined, enter in the code:

    "P&rint"

    That's it.

  2. 15 Jul 2005 at 22:27
    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 properties->appearance->effects

    and uncheck the "Hide underlined charactors for keyboard navigation until I press the ALT key" checkbox.

    these instructions are for XP SP2 Pro build 2600
  3. 15 May 2003 at 06:43

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

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Introduction to Windows Forms.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related discussion

Events coming up

  • Aug 28

    St. Louis Day of .NET

    St. Charles, United States

    Technical conference with be 2 full days of content with over 40 sessions from local and national speakers, with topics such as:•.NET languages: C#, VB.NET•Technologies: WPF, Silverlight, WCF•Development tools: Visual Studio, TFS, Expression Blend

We'd love to hear what you think! Submit ideas or give us feedback