Windows Forms Basics


Windows Forms is the framework for building Windows applications. It enables you to construct an application from scratch in a relatively short period of time. For those who started programming applications for Windows when it was introduced, the fact that this can be accomplished with minimal effort and time is something of a small miracle. It used to take a few days just to construct the basic framework for an application (assuming that you didn't have a copy of a previous starting application saved as a pseudo-template). Now, depending on the speed of your computer, that same task takes only seconds. In fact, if you have never done any programming before, you can get a Windows Forms application up and running with little guidance from anyone else.

However, just because an application runs, that does not make it a good application. There are other things to consider when making that determination. The first factor is whether the application performs the function for which it was intended. The next consideration is how much effort it takes to maintain the application. Other aspects must also be considered, such as whether the code is readable, and so on. Without a basic understanding of how things are done in a Windows Forms application, it is impossible to create a well-formed application for which all the previous questions can be answered yes. In the following sections, you will gain a good understanding of the basics of Windows Forms programming.

Introducing the Main Method

Every Windows Forms application created in C# must contain a static method called Main. This method serves as the starting point for every application and serves as a pseudo-control point for this application. Because the Main method serves as the entry point, if you omit it, your application cannot run and the compiler will return an error that looks something like the following message:

 Program 'F:\Visual C#.NET Unleashed\Chapter 23\Code Examples\HelloWorld\obj\Debug\ HelloWorld.exe'  does not have an entry point defined. 

The Main method is a static method that can return either an int or void type and can be declared with or without parameters. Without any parameters, the Main method takes one of the following forms:

 static int Main() static void Main() 

If you want to allow your program to receive command-line arguments, simply use one of the following forms of the Main method:

 static int Main(string[] args) static void Main(string[] args) 

To run a Windows Forms application, you must make a call to the Application.Run method. Application.Run takes the following forms, which are excerpted from Visual Studio .NET's help file:

 // Begins running a standard application message loop on // the current thread, without a form. public static void Run(); // Begins running a standard application message loop on // the current thread, with an ApplicationContext public static void Run(ApplicationContext); // Begins running a standard application message loop on // the current thread, and makes the specified form visible public static void Run(Form); 

In C#, the Main method can be placed in any object that you choose, but there can be only one occurrence of this method per application. If you have multiple Main methods in an application, you will receive a compiler error similar to the following:

 F:\Visual C#.NET Unleashed\Chapter 23\Code Examples\HelloWorld\Form1.cs(70): Program 'F:\Visual C#.NET Unleashed\Chapter 23\Code Examples\HelloWorld\obj\Debug\ HelloWorld.exe' has more than one entry point defined: 'HelloWorld.Form1.Main()' 

The following code snippet shows what a simple Main method for a Windows Forms application could look like:

 [STAThread] static void Main() {   Application.Run(new Form1()); } 

Understanding the Forms Designer

In a way, the Forms Designer is similar to the Resource Editor that was used in the early days of Windows application development, or if you come from Visual C++, the not-so-distant past. The Forms Designer is essentially a graphical display/interface of the GUI code that is constructed within the code file. You can drag and drop components and controls from the toolbox to the form (the easy way) or create all the necessary code inside the code file so that the information will appear in the Forms Designer. Figure 15.1 shows an example of a Button control that has been dragged and dropped on the form.

Figure 15.1. Design view of a newly created button on a Windows Form.


Creating the Button control took only a few seconds to do and was not difficult to do by any stretch of the imagination. The following code snippet shows all the code that the Windows Forms Designer created in order for this Button to appear in the correct location on the form and with the appropriate properties:

 #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() {   this.button1 = new System.Windows.Forms.Button();   this.SuspendLayout();   //   // button1   //   this.button1.Location = new System.Drawing.Point(120, 96);   this.button1.Name = "button1";   this.button1.TabIndex = 0;   this.button1.Text = "button1";   //   // Form1   //   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);   this.ClientSize = new System.Drawing.Size(360, 266);   this.Controls.Add(this.button1);   this.Name = "Form1";   this.Text = "Form1";   this.ResumeLayout(false); } #endregion 

In this snippet, you can see that the text of the button is set to button1. If you change the text in the code file to This is your button! and then switch to the Design View window, you will see the change reflected on the button (as shown in Figure 15.2).

Figure 15.2. Design view of a button with the Text property changed from the Code View window.


If you use the Properties toolbar of the Design View window to change the Text property of the Button control to Changed Again! and go back to the Code View window, you will see that change reflected in the code:

 //   // button1   //   this.button1.Location = new System.Drawing.Point(120, 96);   this.button1.Name = "button1";   this.button1.TabIndex = 0;   this.button1.Text = "Changed Again!"; 



    Visual C#. NET 2003 Unleashed
    Visual C#. NET 2003 Unleashed
    ISBN: 672326760
    EAN: N/A
    Year: 2003
    Pages: 316

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