Difference between UserControl and CustomControl project templates

  • Thread starter Thread starter Alexander Mueller
  • Start date Start date
A

Alexander Mueller

Hi

In VS 2008 Pro I can choose whether to create a Custom Control or
a User Control, when creating a new project and browsing the
WindowsForms-section.
What's the difference between the two project-templates?

I want to create simple reusable container to perform
text-searches.
It's simply a groupbox with a textbox and a button inside,
that listens to textbox_change and button_click.
Is a UserControl better then a Custom Control for this purpose?

Any hints highly appreciated!

Alex
 
Yes, definitely.

Why definitely? A Groupbox is a container control so why not inherit from
Groupbox and lose the unnecessary extra container of usercontrol.
If you want a Design Surface for the Groupbox so that you can add other
controls to it then simply hijack the UserControlDesigner in a base class
and then inherit from that base class as in the example below:

/// Start Code example ////////////////////////////////////////

public class MyGroupBox : MyGroupBoxBase
{
private TextBox textBox1;
private Button button1;

public MyGroupBox()
: base()
{
InitializeComponent();
}

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(117, 57);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(77, 27);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(6, 25);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(188, 26);
this.textBox1.TabIndex = 1;
//
// MyGroupBox
//
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "MyGroupBox";
this.Size = new System.Drawing.Size(200, 95);
this.ResumeLayout(false);
this.PerformLayout();

}

}

[Designer("System.Windows.Forms.Design.UserControlDocumentDesigner,
System.Design", typeof(IRootDesigner))]
public class MyGroupBoxBase : GroupBox
{
}

/// End Code example ////////////////////////////////////////

note that the base class code can be added to the same class, but it must
appear after the class which you want to use.
 
If you're using .NET and a virtual machine then you're hardly going to be
hyper-optimising to the extent of losing an "unnecessary extra container"
that the docs recommend and expect you to use for this kind of thing.
I didn't mean to attack you, although it may have appeared that way, I'm
just questioning the definitely statement.

Which is better depends entirely upon the intended use, but I would say that
if you're basing your control on a GroupBox, then a custom control is the
better option as GroupBox is already a container control. In most other
cases a UserControl would be better as you need the container to group the
other controls.

If you're only going to use one or two of these controls, then it won't make
much difference either way, but if you're going to use several of them (and
I would assume that if you're going to the effort of making a custom control
out of it that you don't want just one or two), then each one of them has an
extra Window Handle and an extra Paint/PaintBackground message which use up
resources unnecessarily. I don't know anything about the app and it may make
no real difference at all if more resources are used, but if you can save
them this easily then why not do so?
Seems wildly overcomplicated to me for just a couple of textboxes. But
yes, that works too.
It's just three extra lines of code to define the base class, how is that
wildly overcomplicated?
 
Back
Top