Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I notice in some projects (in Code Project and others), the developers move the "InitializeComponent" and "Dispose" to Form Code (put it in the form)

Why they do so ? and how i can benefit from this move ?


Example :

[][Sticky Windows]

Form 1 :


XML
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

        //Blue.Windows.StickyWindow.RegisterExternalReferenceForm ( this );
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (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.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(10, 16);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(102, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "New Test Form";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(350, 53);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Form2 ff = new Form2();
            ff.Show();
        }
    }
}





Form 2 :



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using Blue.Windows;

namespace WindowsApplication1
{
///
/// Summary description for Form2.
///

public class Form2 : System.Windows.Forms.Form
{
private StickyWindow stickyWindow;

private System.Windows.Forms.CheckBox checkStickToScreen;
private System.Windows.Forms.CheckBox checkStickToOthers;
private System.Windows.Forms.CheckBox checkStickOnResize;
private System.Windows.Forms.CheckBox checkStickOnMove;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public Form2()
{
InitializeComponent();
stickyWindow = new StickyWindow( this );
checkStickOnMove.Checked = stickyWindow.StickOnMove;
checkStickOnResize.Checked = stickyWindow.StickOnResize;
checkStickToOthers.Checked = stickyWindow.StickToOther;
checkStickToScreen.Checked = stickyWindow.StickToScreen;
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.checkStickToScreen = new System.Windows.Forms.CheckBox();
this.checkStickToOthers = new System.Windows.Forms.CheckBox();
this.checkStickOnResize = new System.Windows.Forms.CheckBox();
this.checkStickOnMove = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// checkStickToScreen
//
this.checkStickToScreen.Location = new System.Drawing.Point(22, 26);
this.checkStickToScreen.Name = "checkStickToScreen";
this.checkStickToScreen.TabIndex = 0;
this.checkStickToScreen.Text = "Stick to Screen";
this.checkStickToScreen.CheckedChanged += new System.EventHandler(this.checkStickToScreen_CheckedChanged);
//
// checkStickToOthers
//
this.checkStickToOthers.Location = new System.Drawing.Point(22, 58);
this.checkStickToOthers.Name = "checkStickToOthers";
this.checkStickToOthers.TabIndex = 1;
this.checkStickToOthers.Text = "Stick to Others";
this.checkStickToOthers.CheckedChanged += new System.EventHandler(this.checkStickToOthers_CheckedChanged);
//
// checkStickOnResize
//
this.checkStickOnResize.Location = new System.Drawing.Point(22, 122);
this.checkStickOnResize.Name = "checkStickOnResize";
this.checkStickOnResize.TabIndex = 2;
this.checkStickOnResize.Text = "Stick on Resize";
this.checkStickOnResize.CheckedChanged += new System.EventHandler(this.checkStickOnResize_CheckedChanged);
//
// checkStickOnMove
//
this.checkStickOnMove.Location = new System.Drawing.Point(22, 154);
this.checkStickOnMove.Name = "checkStickOnMove";
this.checkStickOnMove.TabIndex = 3;
this.checkStickOnMove.Text = "Stick On Move";
this.checkStickOnMove.CheckedChanged += new System.EventHandler(this.checkStickOnMove_CheckedChanged);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.checkStickOnMove);
this.Controls.Add(this.checkStickOnResize);
this.Controls.Add(this.checkStickToOthers);
this.Controls.Add(this.checkStickToScreen);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);

}
#endregion

private void Form2_Load(object sender, System.EventArgs e)
{
}

private void checkStickToScreen_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickToScreen = checkStickToScreen.Checked;
}

private void checkStickToOthers_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickToOther = checkStickToOthers.Checked;
}

private void checkStickOnResize_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickOnResize = checkStickOnResize.Checked;
}

private void checkStickOnMove_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickOnMove = checkStickOnMove.Checked;
}
}
}
Posted
Updated 17-Nov-13 12:34pm
v10
Comments
CHill60 17-Nov-13 18:08pm    
It might be an idea to actually post links to the articles you refer to. You would also be better off posting a question in a comment to those articles to ask the authors why they have done this. As your question stands I have no idea what you are on about
Ahmad Al Halabi 17-Nov-13 18:20pm    
Sorry about that, i write an example.
CHill60 17-Nov-13 18:20pm    
Where did you find this example?
Ahmad Al Halabi 17-Nov-13 18:23pm    
http://www.codeproject.com/Articles/6045/Sticky-Windows-How-to-make-your-top-level-forms-to
CHill60 17-Nov-13 18:34pm    
Well that article was written back in 2004 when C# was a youngster at version 1.1. I wasn't using C# back then, but it may have been a feature of the IDE at the time. Separating generated code from developer written code strikes me as a pretty good idea, so I can't advise you to start moving stuff around for the sake of it.
Hope these thoughts help

1 solution

There's no mystery here: beginning with C# 2.0 Partial Classes were added to the language and FrameWork. You can even use Partial to "spread" out Structs, Interfaces, and Methods: although, why you'd want to do that is something I'll never understand.

Partial Classes are nothing new under the sun; SmallTalk had them in the late stone-age: they were called "Class Extensions."

They are primarily a convenience for developers, and a way to cleanly separate automatically generated code from code you write.

So, the WinForms code you look at where the programmer's code is mixed-in with the Form Control creation, and layout, and InitializeComponents is defined, etc., is simply old code probably written before C# 2.0.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Nov-13 21:16pm    
All correct, a 5.
I would add that it can be beneficial to use partial classes in other cases. I, for example, routinely add additional file with partial declarations of the form class, leaving auto-generated code nearly untouched. (I add a call to just one method from the form constructor right after InitializeComponent and define this method in additional part.) It organizes my code and workflow a lot.
—SA
BillWoodruff 17-Nov-13 22:52pm    
Good point, Sergey; I didn't mean to sound like I think people should not use 'Partial syntax.

I also imagine that in a team programming environment dividing up Classes using 'Partial is likely to be valuable, but I do not have any experience with this myself.
Sergey Alexandrovich Kryukov 17-Nov-13 22:59pm    
As you can see, there is nothing special about it. It's majorly the way people can organize the text of the source code.
However, there is an important abstraction feature: note that a part does not have to show all the inheritance list. Say, you want to implement some interface. Declare class MyType : MyBaseType {...} in one part, MyType : IMyInterface {...} in another. The actual class inherit form the union of the list, but the parts "don't see each other", in this respect. This is a powerful feature organizing code in a neat way...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900