1.2 Create a Windows-Based Application


Problem

You need to build an application that provides a Windows Forms “based GUI.

Solution

Ensure you implement a static method named Main in at least one of your source code files. In the Main method, create an instance of a class that extends the System.Windows.Forms.Form class. (This is your application's main form.) Pass your main form object to the static method Run of the System.Windows.Forms.Application class. Use the /target:winexe switch on the C# compiler (csc.exe) when you compile your assembly.

Discussion

Building an application that provides a simple Windows GUI is a world away from the development of a full-fledged Windows-based application. However, there are things you must do regardless of whether you are writing the Windows equivalent of Hello World or the next version of Microsoft Word, including the following:

  • For each form you need in your application, create a class that extends the System.Windows.Forms.Form class.

  • In each of your form classes, declare members that represent the controls that will be on that form, for example buttons , labels, lists, and text boxes. These members should be declared private or at least protected so that other program elements can't access them directly. If you need to expose the methods or properties of these controls, implement the necessary members in your form class, providing indirect and controlled access to the contained controls.

  • Declare methods in your form class that will handle events raised by the controls contained by the form, such as button clicks or key presses when a text box is the active control. These methods should be private or protected and follow the standard .NET event pattern (described in recipe 16.10). It's in these methods (or methods called by these methods) where you will define the bulk of your application's functionality.

  • Declare a constructor for your form class that instantiates each of the form's controls and configures their initial state ( size , color , position, content, and so on). The constructor should also wire up the appropriate event handler methods of your class to the events of each control.

  • Declare a static method named Main ” usually as a member of your application's main form class. This method is the entry point for your application, and it can have the same signatures as those mentioned in recipe 1.1. In the Main method, create an instance of your application's main form and pass it as an argument to the static Application.Run method. The Run method makes your main form visible and starts a standard Windows message loop on the current thread, which passes the user input (key presses, mouse clicks, etc.) to your application as events.

The WelcomeForm class shown in the following code listing is a simple Windows Forms application that demonstrates the techniques just listed. When run, it prompts a user to enter a name and then displays a message box welcoming the user to the C# Programmer's Cookbook .

 using System.Windows.Forms; public class WelcomeForm : Form {     // Private members to hold references to the form's controls.     private Label label1;     private TextBox textBox1;     private Button button1;     // Constructor used to create an instance of the form and configure     // the form's controls.     public WelcomeForm() {         // Instantiate the controls used on the form.         this.label1 = new Label();         this.textBox1 = new TextBox();         this.button1 = new Button();         // Suspend the layout logic of the form while we configure and          // position the controls.         this.SuspendLayout();         // Configure label1, which displays the user prompt.         this.label1.Location = new System.Drawing.Point(16, 36);         this.label1.Name = "label1";         this.label1.Size = new System.Drawing.Size(128, 16);         this.label1.TabIndex = 0;         this.label1.Text = "Please enter your name:";         // Configure textBox1, which accepts the user input.         this.textBox1.Location = new System.Drawing.Point(152, 32);         this.textBox1.Name = "textBox1";         this.textBox1.TabIndex = 1;         this.textBox1.Text = "";         // Configure button1, which the user presses to enter their name.         this.button1.Location = new System.Drawing.Point(109, 80);         this.button1.Name = "button1";         this.button1.TabIndex = 2;         this.button1.Text = "Enter";         this.button1.Click += new System.EventHandler(this.button1_Click);         // Configure WelcomeForm and add controls.         this.ClientSize = new System.Drawing.Size(292, 126);         this.Controls.Add(this.button1);         this.Controls.Add(this.textBox1);         this.Controls.Add(this.label1);         this.Name = "form1";         this.Text = "C# Programmer's Cookbook";         // Resume the layout logic of the form now that all controls are          // configured.         this.ResumeLayout(false);     }     // Application entry point, creates an instance of the form, and begins     // running a standard message loop on the current thread. The message      // loop feeds the application with input from the user as events.     public static void Main() {         Application.Run(new WelcomeForm());     }     // Event handler called when the user clicks the Enter button.     private void button1_Click(object sender, System.EventArgs e) {         // Write debug message to the console         System.Console.WriteLine("User entered: " + textBox1.Text);         // Display welcome as a message box         MessageBox.Show("Welcome to the C# Programmer's Cookbook, "              + textBox1.Text, "C# Programmer's Cookbook");     } } 

To build the WelcomeForm class (contained in a file named WelcomeForm.cs) into an application, use the command csc /target:winexe WelcomeForm.cs . The /target:winexe switch tells the compiler that you are building a Windows-based application. As a result, the compiler builds the executable in such a way that no console is created when you run your application. If you use the /target:exe switch to build a Windows Forms application instead of /target:winexe , your application will still work correctly, but you will have a Console window visible while the application is running. Although this is undesirable for production quality software, the Console window is useful if you want to write debug and logging information while you're developing and testing your Windows Forms application. You can write to this console using the Write and WriteLine methods of the System.Console class.

Figure 1.1 shows the WelcomeForm.exe application in operation greeting a user named Rupert. This version of the application is built using the /target:exe compiler switch, resulting in the visible Console window in which you can see the output from the Console.WriteLine statement in the button1_Click event handler.

click to expand
Figure 1.1: A simple Windows Forms application.
Note  

Building large GUI-based applications is a time consuming undertaking that involves the correct instantiation, configuration, and wiring up of many forms and controls. Microsoft Visual Studio .NET automates much of the work associated with building graphical applications. Trying to build a large graphical application without the aid of tools like Visual Studio .NET will take you much longer, be extremely tedious , and result in a greater chance of bugs in your code.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

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