15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1120 ❘ ChaPTer 39 windOws fOrms<br />

protected override void Dispose(bool disposing)<br />

{<br />

if (disposing && (components != null))<br />

{<br />

components.Dispose();<br />

}<br />

base.Dispose(disposing);<br />

}<br />

#region Windows Form Designer generated code<br />

/// <br />

/// Required method for Designer support—do not modify<br />

/// the contents of this method with the code editor.<br />

/// <br />

private void InitializeComponent()<br />

{<br />

this.components =<br />

new System.ComponentModel.Container();<br />

this.AutoScaleMode =<br />

System.Windows.Forms.AutoScaleMode.Font;<br />

this.Text = "Form1";<br />

}<br />

#endregion<br />

}<br />

}<br />

code snippet Form1.Designer.cs<br />

The Designer file of a form should rarely be edited directly. The only exception is if there is any special<br />

processing that needs to take place in the Dispose method. The InitializeComponent method is discussed<br />

later in this chapter.<br />

Looking at the code as a whole for this sample application, you can see it is much longer than the simple<br />

comm<strong>and</strong>-line example. There are several using statements at the start of the class; most are not necessary<br />

for this example. There is no penalty for keeping them there. The class Form1 is derived from System<br />

.Windows.Forms.Form just like the earlier Notepad example, but things start to get different at this point.<br />

First, there is this line in the Form1.Designer file:<br />

private System.ComponentModel.IContainer components = null;<br />

In the example, this line of code doesn’t really do anything. When you add a component to a form, you can<br />

also add it to the components object, which is a container. The reason for adding to this container has to do<br />

with disposing of the form. The form class supports the IDisposable interface because it is implemented in<br />

the Component class. When a component is added to the components object, this container will make sure<br />

that the components are tracked properly <strong>and</strong> disposed of when the form is disposed of. You can see this if<br />

you look at the Dispose method in the code:<br />

protected override void Dispose(bool disposing)<br />

{<br />

if (disposing && (components != null))<br />

{<br />

components.Dispose();<br />

}<br />

base.Dispose(disposing);<br />

}<br />

Here you can see that when the Dispose method is called, the Dispose method of the components object is<br />

also called, <strong>and</strong> because the component object contains the other components, they are also disposed of.<br />

The constructor of the Form1 class, which is in the Form1.cs file, looks like this:<br />

public Form1()<br />

{<br />

InitializeComponent();<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!