开发者

Why partial classes when I create a brand new custom control in winforms?

There are a few ways to start creating a new custom control in VS? Which one should I use? As in:

Right click project -> Add ->
1. User Control, or
2. Component, or
3. Custom Control, or?

I use #1 and get a partial class similar to:

namespace MyControls
{
    partial class NewControl
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        privat开发者_C百科e 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 Component 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 ( )
        {
            components = new System.ComponentModel.Container ( );
            //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
    }

Why do I need this class? Is it only useful for composite controls where you need to use the designer?

EDIT: I know about the partial keyword and classes, but I don't know why I need this particular class for my custom control. It doesn't seem to make a difference.


This class contains the InitializeComponent method, which is generated by the designer and contains all of the code necessary to create what you've made in the designer.

If you don't use the component designer at all, it is perfectly OK to delete this file (and the ResX file, if any) and remove the partial keyword from your class declaration.

Note that deleting this file will permanently destroy anything you've made in the designer.


Partial classes let you add your custom code to the class without having to modify the file that the IDE generates. This avoids problems that are otherwise common where you might tweak an IDE generated file only to have your changes clobbered when the IDE found the need to update what it generated.


In addition to what Michael Burr said, this helps hide the designer implementation from your view, so you're less likely to make a change that will render the designer unable to parse your file.

IMHO, they also help keep your code files clean by hiding clutter that's simply used to implement the GUI of the control. You can focus on the other details that makes the control work the way you want.


It lets the designer have an entire file that it can make changes to at will without fear of damaging your code. I suspect this makes it easier to implement the designer so that it doesn't have to watch for and "dance around" those comments referred to in jasonh's comment.


Yes, it's for when you want to use the designer to create your control. Just like with winforms, where you want your custom code to avoid being lost when the designer re-generates the .designer.cs file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜