When you create a new form in Visual Studio, the designer generates the following code in the .Designer.cs file:
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
What is the purpose of the components
variable? My theory is that I should use it for any IDisposable
classes my form owns which I create outside of Designer (since Dispose
is already being implemented by the Designer).
So, for example, if my form owns a font, I could make sure it gets disposed by adding it to components
like this:
public partial class Form1 : Form
{
Font coolFont;
public Form1()
{
InitializeComponent();
this.coolFont = new Font("Comic Sans", 12);
components.Add(this.coolFont);
}
}
Is that what it's for? I haven't been able to find any documentation or information about this.
question from:
https://stackoverflow.com/questions/5069391/whats-the-purpose-of-the-components-icontainer-generated-by-the-winforms-design 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…