7.1 Creating a Dialog-Based Windows Form

 <  Day Day Up  >  

You want to create a simple Windows Form application.


Technique

Create a new Windows Forms application by selecting New, Project from the main menu. Select the Visual C# Projects item in the project types list and select the Windows Application project template.

Comments

One of the first things you'll see after creating a Windows Forms project is the Windows Form designer. The designer is a user -interface editor that allows you to change various aspects of the user interface, including changing form properties, adding and manipulating controls, and adding various event handlers.

One aspect of Windows Forms that is different from previous Microsoft user-interface technologies is the absence of a separate resource file containing the user-interface definition. Rather than create a special file format to describe a user interface, the Windows Form editor directly changes the underlying code within your application. To see the code that was generated when you created the project, select View, Code from the main menu while in the designer, right-clicking on Form1.cs in the Solution Explorer or on the forms designer itself and selecting the View Code menu item.

You create a Windows Form simply by deriving a class from the System.Windows.Forms.Form base class, as shown in Listing 7.1. When you create a Windows Form application, the code generates this class with a default constructor, an overridden method, and a private method named InitializeComponent , which is called within the constructor. Due to the outlining features of the Visual Studio Integrated Development Environment (IDE), you cannot initially see the InitializeComponent method. This method is to appear only in the designer, and as such it is collapsed within an outline region. To view the InitializeComponent method, expand the outlined region named Windows Form Designer Generated Code by double-clicking it. Whenever you make a change to the user interface within the designer, the designer makes the corresponding changes within this code region. That said, you must be aware that any changes you manually make within this method might be overwritten by the designer itself.

Listing 7.1 A Basic Windows Form
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace _1_BasicForm {     public class Form1 : System.Windows.Forms.Form     {         private System.ComponentModel.Container components = null;         public Form1()         {             InitializeComponent();         }         protected override void Dispose( bool disposing )         {             if( disposing )             {                 if (components != null)                 {                     components.Dispose();                 }             }             base.Dispose( disposing );         }         #region Windows Form Designer generated code         private void InitializeComponent()         {             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);             this.ClientSize = new System.Drawing.Size(292, 266);             this.Name = "Form1";             this.ShowInTaskbar = false;             this.Text = "My Windows Form";         }         #endregion         [STAThread]         static void Main()         {             Application.Run(new Form1());         }     } } 

The class contains a single overridden method named Dispose . You use this method to release any resources your class is holding and to perform any type of processing necessary before closing the form. The parameter for this method is a Boolean value named disposing , which at its surface seems a little misleading. When this parameter is true , you should release any managed resources that your class is holding. When this parameter is false , the semantics lead you to believe that the class does not need to dispose or release any resources, but that is not true. When disposing is false , you are to free any unmanaged resources, which is a subject covered in Chapter 24, "COM Interoperability." Also, the runtime does not guarantee that the Dispose method will be called only once when the form is closed, so you must ensure that you don't inadvertently release a resource that has already been released, thereby resulting in an exception.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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