![]() |
< Day Day Up > |
![]() |
6.1. Programming a Windows FormAll 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?span class="docEmphasis">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 HandLet'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 Handusing 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 applicationThe code breaks logically into three sections:
Core Note
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 > |
![]() |