Nishant
Nishant

Reputation: 905

.NET Windows Forms Anatomy

Can anybody help me understand the anatomy of a Windows Form (.net)?

When I create a new Windows Form from my .NET application, I can see in the designer class some basic amount of code generated by the studio. I have just copy pasted it below.

namespace FormBasic
{
    partial class Form1
    {
        /// <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);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion
    }
}

Of them what I found interesting was this line of code

private System.ComponentModel.IContainer components = null;

What exactly is this Container? I debugged by placing a break point in the InitializeComponent function. The components remain null.

Also the hierarchy of the Form class is Form class Inherits from ContainerControl class. Can you please help me to clear these terminologies?

Upvotes: 2

Views: 596

Answers (2)

Greg Buehler
Greg Buehler

Reputation: 3894

The line private System.ComponentModel.IContainer components = null; facilitates the management of items like Timer or BackgroundWorker in the "Components" section of the toolbox. In the editor, they show up like this:

enter image description here

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292555

The components field is a container for non-visual components (like OpenFileDialog, ErrorProvider, FileSystemWatcher...). It's only initialized if you have such components on your form. It's a way to ensure that these components are disposed when the form is disposed.

Upvotes: 4

Related Questions