Tip

Understand Windows forms


.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

    Requires Free Membership to View

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 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

    Calling Dispose on a nonmodal form doesn't call the Closing and Closed events, so use caution when calling Dispose—you might miss your cleanup code!

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

Public Sub New()
  MyBase.New()

  'This call is required by the Windows Form Designer.
  InitializeComponent()

  'Add any initialization after the InitializeComponent() call
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  If disposing Then
    If Not (components Is Nothing) Then
      components.Dispose()
    End If
  End If
  MyBase.Dispose(disposing)
End Sub

C#

public frmMain()
{
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
    if( disposing )
    {
    if (components != null)
        {
            components.Dispose();
        }
    }
    base.Dispose( disposing );
}

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:

  • Looking for free research? Browse our comprehensive White Papers section by topic, author or keyword.
  • Are you tired of technospeak? The Web Services Advisor column uses plain talk and avoids the hype.
  • For insightful opinion and commentary from today's industry leaders, read our Guest Commentary columns.
  • Hey Codeheads! Start benefiting from these time-saving XML Developer Tips and .NET Developer Tips.

  • Visit our huge Best Web Links for Web Services collection for the freshest editor-selected resources.
  • Visit Ask the Experts for answers to your Web services, SOAP, WSDL, XML, .NET, Java and EAI questions.
  • Choking on the alphabet soup of industry acronyms? Visit our helpful Glossary for the latest industry lingo.
  • Couldn't attend one of our Webcasts? Don't miss out. Visit our archive to watch at your own convenience.
  • Discuss this article, voice your opinion or talk with your peers in the SearchWebServices Discussion Forums.

This was first published in April 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.