.NET Developer Tip
(Receive this column in your inbox,
click Edit your Profile to subscribe.)
Understand Windows forms
Jason Beres
Forms are a basic reason to use the .NET framework. But to use the forms capability of the platform, you have to understand forms, how they work, and their properties. This tip, excerpted from InformIT, gives you a start on understanding these aspects of forms in your .NET development.
The tip references an application called HelloNET, which is found in the larger article at InformIT.
Understand Windows Forms
Each form you add to a Windows Forms application is simply a class file. In fact, every single object that exists in the .NET Framework is a class file. What differentiates each class file is the base class that it inherits. Visual Basic .NET and C# are object-oriented programming languages, and as you learned on Day 1, "Introduction to the Microsoft .NET Framework," all languages in .NET are on an equal standing with each other. This is because they all share the same base classes in the framework class library. A Windows Form is a Windows Form because it inherits its functionality from the System.Windows.Forms namespace. This namespace is used across all .NET languages that must implement Windows Forms applications. If you double-click Form1 of the HelloNET application, you'll see the following code, which tells the class file it's a Windows Form:
VB.NET
Public Class frmMain
Inherits System.Windows.Forms.Form
C#
public class frmMain : System.Windows.Forms.Form
The class name in this case is frmMain, which is the Name property of the form you set in the Properties window. The class file is inheriting the System.Windows.Forms.Form class. By inheriting this class, the frmMain class now has all the base class events
To continue reading for free, register below or login
To read more you must become a member of SearchSOA.com
');
// -->

that you saw earlier when you learned how to add events to the frmMain form. Anytime you inherit a class, you're exposing its functionality to your class.
When working with classes, an object lifetime is predefined by events that are part of all objects. Because a form is a class, it has predefined events that dictate its lifetime. The difference between a Windows Form and a class in a DLL is that a user decides when to close a form. In a DLL, a class instance is created, some code is run, and the class instance is destroyed. To understand this better and to get an understanding of the lifetime of a Windows Form, lets examine the lifecycle of an object.
The Life Cycle of an Object
Every object has a constructor that fires when an object is instantiated and a destructor that fires before it's destroyed.
An object's life begins when an instance of a class is created using the New keyword. New objects often require initialization tasks to be performed before they're used for the first time. Some common initialization tasks include connecting to a database, opening files, or checking configuration settings. In Windows Forms, this initialization occurs in the constructor for the form, which is the Sub New event in VB.NET and the Main event in C#. In VB.NET, when the Sub New event is fired, the InitializeComponent method is called, which handles the initialization of the form's controls. In the InitializeComponent event, controls you added at design time are created and the properties of those controls are set. In C#, the Main method creates the class instance by calling the Application.Run method, which passes a new instance of the class being created. This is the form that will load first in a C# application.
When an object is destroyed, the memory it was consuming is given back to the operating system. In Windows Forms, the Closed event calls the destructor for forms that aren't shown modally. If you're showing a form modally, you'll need to call the Dispose method of the form to release the form's memory back to the operating system.
NOTE
To see this happening in your application, drill into the Windows Form Designer generated code region in frmMain. You'll see New and Dispose events shown in Listing 3.3. Notice the New method calls the InitializeComponent method.
Listing 3.3 Examining Sub New and Sub Dispose in Windows Forms
VB.NET
C#
To read the entire article from which this tip is excerpted, click over to InformIT. You have to register there, but the registration is free.
For More Information: