6.1. Programming a Windows Form

 < Day Day Up > 

All Windows Forms programs begin execution at a designated main window. This window is actually a Form object that inherits from the System.Windows. Forms.Form class. The initial window is displayed by passing an instance of it to the static Application.Run method.

The challenge for the developer is to craft an interface that meets the basic rule of design form follows function. This means that a form's design should support its functionality to the greatest extent possible. To achieve this, a developer must understand the properties, methods, and events of the Form class, as well as those of the individual controls that are placed on the form.

Building a Windows Forms Application by Hand

Let's create a simple Windows application using a text editor and the C# compiler from the command line. This application, shown in Listing 6-1, consists of a single window with a button that pops up a message when the button is clicked. The simple exercise demonstrates how to create a form, add a control to it, and set up an event handler to respond to an event fired by the control.

Listing 6-1. Basic Windows Forms Application Built by Hand
 using System; using System.Windows.Forms; using System.Drawing; class MyWinApp {    static void Main()     {    // (1) Create form and invoke it    Form mainForm = new SimpleForm();    Application.Run(mainForm);      } } // User Form derived from base class Form class SimpleForm:Form {    private Button button1;    public SimpleForm() {       this.Text = "Hand Made Form";       // (2) Create a button control and set some attributes       button1 = new Button();       button1.Location = new Point(96,112);       button1.Size = new Size(72,24);       button1.Text= "Status";       this.Controls.Add(button1);       // (3) Create delegate to call routine when click occurs       button1.Click += new EventHandler(button1_Click);    }    void button1_Click(object sender, EventArgs e) {       MessageBox.Show("Up and Running");    } } 

Recall from Chapter 1, "Introduction to .NET and C#," that command-line compilation requires providing a target output file and a reference to any required assemblies. In this case, we include the System.Windows.Forms assembly that contains the necessary WinForms classes. To compile, save the preceding source code as winform.cs and enter the following statement at the command prompt:

 csc /t:winform.exe /r:System.Windows.Forms.dll winform.cs 

After it compiles, run the program by typing winform; the screen shown in Figure 6-1 should appear. The output consists of a parent form and a second form created by clicking the button. An important point to note is that the parent form cannot be accessed as long as the second window is open. This is an example of a modal form, where only the last form opened can be accessed. The alternative is a modeless form, in which a parent window spawns a child window and the user can access either the parent or child window(s). Both of these are discussed later.

Figure 6-1. Introductory Windows application


The code breaks logically into three sections:

  1. Form Creation.

    The parent form is an instance of the class SimpleForm, which inherits from Form and defines the form's custom features. The form and program is invoked by passing the instance to the Application.Run method.

  2. Create Button Control.

    A control is placed on a form by creating an instance of the control and adding it to the form. Each form has a Controls property that returns a Control.Collection type that represents the collection of controls contained on a form. In this example, the Controls.Add method is used to add a button to the form. Note that a corresponding Remove method is also available to dynamically remove a control from its containing form. An IDE uses this same Add method when you drag a control onto a form at design time. However, if you want to add or delete controls at runtime, you will be responsible for the coding.

    Controls have a number of properties that govern their appearance. The two most basic ones are Size and Location. They are implemented as:

     button1.Size = new Size(72,24);   // width, height button1.Location = new Point(96,112); //x,y 

    The struct Size has a constructor that takes the width and height as parameters; the constructor for Point accepts the x and y coordinates of the button within the container.

  3. Handle Button Click Event.

    Event handling requires providing a method to respond to the event and creating a delegate that invokes the method when the event occurs (refer to Chapter 3, "Class Design in C#," for details of event handling). In this example, button1_Click is the method that processes the event. The delegate associated with the Click event is created with the following statement:

     button1.Click += new EventHandler(button1_Click); 

    This statement creates an instance of the built-in delegate EventHandler and registers the method button1_Click with it.

Core Note

.NET 2.0 adds a feature known as Partial Types, which permits a class to be physically separated into different files. To create a partial class, place the keyword partial in front of class in each file containing a segment of the class. Note that only one class declaration should specify Forms inheritance. The compilation process combines all the files into one class identical to a single physical class. For Windows applications, partial classes seem something of a solution in search of a problem. However, for Web applications, they serve a genuine need, as is discussed in Chapter 16, "ASP.NET Web Forms and Controls."


This exercise should emphasize the fact that working with forms is like working with any other classes in .NET. It requires gaining a familiarity with the class members and using standard C# programming techniques to access them.

     < Day Day Up > 


    Core C# and  .NET
    Core C# and .NET
    ISBN: 131472275
    EAN: N/A
    Year: 2005
    Pages: 219

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net