NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Overview of Win Forms

This section provides a high level overview of the Win Forms framework and how Win Forms controls are used in Win Forms applications.

The Win Forms framework encapsulates native Win32 APIs and exposes secure, managed classes for creating Win32 client-side applications. The Win Forms class library provides many controls such as buttons, check boxes, drop-down lists, combo boxes, data grid, and others that encapsulate user interface and other client-side functionality. Application development is greatly simplified if you use a rapid application development (RAD) environment such as Visual Studio 7.0. In the Visual Studio Win Forms designer, the controls appear in a toolbox and are dropped on a design surface. This is accompanied by automatic code generation. The forms designer has a property window that displays properties and events. Using the property browser, it is easy to modify properties and to provide code for event handling methods. The Visual Studio 7 Win Forms designer thus generates all the generic code for an application developer, so that you can focus on writing the code that is specific to your application. While a designer simplifies application development, the Win Forms framework is designed so that it is not difficult to develop applications all in code, without using a designer.

Every Win Forms application

The Application.Run method processes messages from the operating system to the application.

Many examples of Win Forms applications are provided in the Win Forms Quickstart.

The code example below shows the essential elements of a Win Forms application.

[C#]
using System;
using System.WinForms;
// Every Win Forms application derives from System.WinForms.Form
public class MyForm : Form {

   public static int Main(string[] args) {
     MyForm aform = new MyForm();

// The Application.Run method processes messages from the operating system to // your application. If you comment out the next line of code 
// and simply instantiate the form in Main
// your application will compile and execute, but because it is not in the // message loop, it will exit after the form is instantiated.
      Application.Run(aform);
      return 0;
   }

   public MyForm() {
      this.Text = "Hello World";
   }
}

The code example below shows how controls are used in a Win Forms application, and how events are handled. The example consists of three buttons on a form; each button changes the background to a different color when clicked.

[C#]
//Simple Win Forms application that shows how controls
//are used and how events are handled.
using System;
using System.ComponentModel;
using System.WinForms;
//using System.Resources;
using System.Drawing;

public class MyForm : System.WinForms.Form {
   
   private System.WinForms.Button red;
   private System.WinForms.Button blue;
   private System.WinForms.Button green;

   public MyForm() : base() {
// InitializeComponent is a helper method. You can do 
// all the work here if you want. InitializeComponent is used 
// in this example for consistency with the Visual Studio 7.0 
// Win Forms designer.   

      InitializeComponent();   
   }

   public override void Dispose() {
      base.Dispose();
      //components.Dispose();
   }
   
// Helper method for the constructor.
   private void InitializeComponent() {   
//      components = new System.ComponentModel.Container();

// Create a delegate for the click event of a button. The argument to 
// the constructor contains a reference to the method that performs the // // event handling logic.
      EventHandler handler = new EventHandler(buttonClick);

//    Text, Location, and Size are properties of a Button control. These 
// are set using the property window in the Win Forms designer.
      red = new Button();
      red.Text = "Red";
      red.Location = new System.Drawing.Point(100, 50);
      red.Size = new System.Drawing.Size(50, 50);

//The AddOnClick method registers the delegate with the click event
//of the button.
      red.AddOnClick(handler);

//The Form class has a Controls property that is a collection of child
//controls. The Add method adds your control to that collection.
      Controls.Add(red);
      
      blue = new Button();
      blue.Text = "Blue";
      blue.Location = new System.Drawing.Point(100, 100);
      blue.Size = new System.Drawing.Size(50, 50);
      blue.AddOnClick(handler);
      Controls.Add(blue);
      
      green = new Button();
      green.Text = "Green";
      green.Location = new System.Drawing.Point(100, 150);
      green.Size = new System.Drawing.Size(50, 50);
      green.AddOnClick(handler);
    Controls.Add(green);
      
      
      }
   
//The method that contains the event handling logic   
   private void buttonClick(object sender, EventArgs e) {
            if (sender == red) this.BackColor = Color.Red ;
                  else if (sender == blue) this.BackColor = Color.Blue;
                  else this.BackColor = Color.Green;
        }
   

   public static void Main(string[] args) {
  Application.Run(new MyForm());
   }
   
}