tags:

views:

96

answers:

5

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>
     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 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.

+4  A: 

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.

Michael Burr
A: 

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.

Neil Barnwell
Visual Studio never destroyed your own code, even when the designer generated code lived in the same file as your code. Special comments indicated where the designer was allowed to make changes to the file and it stayed in there.
jasonh
Okay fair enough - I didn't read the question properly. I thought he was referring to writing the control class by hand.
Neil Barnwell
+4  A: 

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.

SLaks
Thanks, this is what I was wondering.
Joan Venge
A: 

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.

RCIX
Waaaay back in the dark, old days of VS .Net, the designer code lived in your file and the designer never plowed over your own code. Some special comments indicated to the designer where it was allowed to modify your file.
jasonh
+3  A: 

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.

jasonh