Removing Winforms Controls

Why is Visual Studio adding this code to the Class.Designer.cs class. Can someone tell me when this component variable gets any value? What picture should follow here?

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);
        }
+5
source share
4 answers

A private field is componentsused to track one-time components in your form. Try dragging the component Timer, and you should see something like this in the code generated by the constructor:

this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);

, Dispose(bool), disposable pattern. , , , Dispose , Dispose ( ).

+4

, (common7\ide\itemtemplates\csharp\windows forms\1033\form.zip\form.designer.cs). , InitializeComponent(), , "this.components" . , - . , .

, , , Dispose() Designer.cs. form.cs, Dispose() , . , . , " ".

, Dispose() , , . , . Form , Controls.

+4

components assigned when adding non-visual components, which must also be removed in a deterministic way (see the IDisposable template for more information).

+1
source

Not all components use it, for example, HelpProvider and EventLog. You can even comment on this in these cases. Looks like I like ms bodge.

0
source

All Articles