Home > Articles > Certification > Microsoft Certification

This chapter is from the book

Understanding 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 );
}

NOTE

Regions are a way of separating code in class files. Using the #Region and #End Region syntax, you can create a tree-like structure of your code in the Code Editor. In Windows Forms and ASP.NET, regions are used to differentiate the code generated by the designer and the code you're writing. You can create your own regions to further define a structure for your code.

The Life Cycle of a Windows Form

To get a handle on the events that occur when you're working with forms, you'll write some code that fires at each stage of a form's lifetime.

To start, add two new forms to your HelloNET application. To do so, right-click on the HelloNET project name in the Solution Explorer and select Add, Add Windows Form from the contextual menu. You're now prompted with the Add New Item dialog shown in Figure 3.9.

Figure 3.9Figure 3.9 Adding a new form to the HelloNET application.

Change the Name to firstForm and click the OK button. You should see firstForm in the Solution Explorer. Next, add another form and name it secondForm.

In the Windows Forms Designer, secondForm is now visible. Your IDE should look something like Figure 3.10.

Figure 3.10Figure 3.10 The HelloNET application after adding two new forms.

Follow these steps to set up the application:

  • Double-click firstForm in the Solution Explorer to make it the active form in the designer.

  • Drag a Button control from the Toolbox onto the form. Change its Name property to showSecondForm and its Text property to Show Second Form.

  • Double-click the showSecondForm button and add the following code to the showSecondForm_click event:

    VB.NET

    MessageBox.Show("Attempting to Load Second Form")
    Dim f As New secondForm()
    MessageBox.Show("Second Form Created")
    f.Show()
    MessageBox.Show("Second Form Loaded")

    C#

    MessageBox.Show("Attempting to Load Second Form");
    secondForm f = new secondForm();
    MessageBox.Show("Second Form Created");
    f.Show();
    MessageBox.Show("Second Form Loaded");
  • Double-click the secondForm file in the Solution Explorer to make it active in the designer. Double-click the form to get to the code-behind class file.

  • Locate the method call to InitializeComponent in the secondForm class file. In Visual Basic .NET, this would be the Sub New event; in C#, syntax does not use a New keyword, it's just the instance declaration of the form. Add the following code to the New event, directly above the call to the InitializeComponent.

    VB.NET

    MessageBox.Show("Initializing from Second Form")

    C#

    MessageBox.Show("Initializing from Second Form");
  • Add the event handler for the Activated event to the secondForm class file. In Visual Basic .NET, you can click the Class Name drop-down box and select (Base Class Events) and then click the Method Name drop-down box and select Activated. In C#, switch to the Form view, press F4 to get the Properties window, and click the Events button to add the Activated event procedure. In the secondForm_Activated event, enter the following code:

    VB.NET

    Me.Text = "Activated from Second Form"

    C#

    this.Text = "Activated from Second Form";
  • Add an event handler for the Load event of the secondForm class file and add the following code:

    VB.NET

    MessageBox.Show("Load from Second Form")

    C#

    MessageBox.Show("Load from Second Form");
  • Add an event handler for the Deactivate event of the secondForm class file and add the following code:

    VB.NET

    Me.Text = "Deactivate from Second Form"

    C#

    this.Text = "Deactivate from Second Form";
  • Add an event handler for the Closing event of the secondForm class file and add the following code:

    VB.NET

    If MessageBox.Show("Are you sure you want to close?", "HelloNET!", _
      MessageBoxButtons.YesNo, MessageBoxIcon.Question) = _
      DialogResult.No Then
    
      e.Cancel = True
      MessageBox.Show("Canceled Closing from Second Form")
    
    Else
      MessageBox.Show("Closing from Second Form")
    End If

    C#

    if (MessageBox.Show ("Are you sure you want to close?", "HelloNET!",
        MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
    {
      e.Cancel = true;
      MessageBox.Show("Canceled Closing from Second Form");
    }
    else
    {
      MessageBox.Show("Closing from Second Form");
    }
  • Add an event handler for the Closed event of the secondForm class file and add the following code:

    VB.NET

    MessageBox.Show("Closed from Second Form")

    C#

    MessageBox.Show("Closed from Second Form");
  • Set the StartUp object for this application to firstForm. You can do this in Visual Basic .NET by right-clicking on the HelloNET project name in the Solution Explorer and selecting Properties from the contextual menu. In the Properties dialog, select firstForm from the StartUp object drop-down list.

  • In C#, change the code in the Sub Main routine of frmMain to look like this:

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

You can finally press the F5 key to run the application. After firstForm is loaded, click the Show Second Form button. You are prompted for each stage of the form's creation. After secondForm in loaded, click back on firstForm. Notice the Text property of the secondForm has changed to Deactivated. Next, click the x on secondForm to close the form. You're prompted with a MessageBox asking whether you want to close the form. This is the same as the QueryUnload event in Visual Basic 6, except that in .NET you're reacting to the CancelEventArgs event. If you click Yes, you'll notice the Closing and Closed events firing. If you click No, the Closing and Closed events don't fire.

Remembering that forms are just classes, click the Show Second Form button again. After the secondForm loads, click the Show Second Form button again. You now should have two instances of secondForm running. As you click between the forms, observe that the Text property of the form changes if the form is or is not active. Because each form is a class, each time you declare a new instance of secondForm, a new copy of the form is made in memory, and the form events occur independent of each other.

To use the events in a real-life scenario, if you need to perform a task every time a form gets the focus, you must put your code in the Activated event. If you want to set some default properties for your controls, you might do it in the form Load event or, if you want to set values for global variables, you could do it in the constructor for the class.

NOTE

In Visual Basic 6, the Activate event occurred when a form was activated within the Visual Basic 6 application it was running in. In Windows Forms, the Activate event is independent of the application. For example, if you maximize Outlook or Word over your form, the Deactivate event occurs. When the form gets the focus again, the Activate event fires.

In the exercise you just completed, secondForm was shown nonmodally. In Visual Basic 6, you passed the vbModal constant to the Show method to display a modal form. In .NET, you simply call ShowDialog instead of the Show method. In this way, messages can be passed back to the form that called the ShowModal. For example, to retrieve a message back from a form shown with ShowDialog, you could use the following code:

If f.ShowDialog() = DialogResult.Cancel Then

The DialogResult property of a Button control determines what value is passed back to the calling form.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020