UserControl vs CustomControl in C#

15,218

Solution 1

There are custom and user controls for both windows applications and web applications. The windows application controls have a .cs extenstion.

In a very general sense a user control is easier to create. You can drag existing controls like textboxes, labels, etc on to the form. Custom controls are generally more difficult (time consuming) to create, but offer greater flexibility, customiseability, and integration.

The major difference in a nutshell is this:

A user control is made up of existing controls. It is also sometimes referred to as a composite control because of this fact. A typical example is a login form. The form and all of the logic is contained within this 'reuseable' user control.

A custom control is a control that you create. In windows forms this means overriding the OnPaint method as in your example above. Custom controls do not have the same level of design time support as user controls do (ie dragging and dropping existing controls, etc). Custom controls are generally thought of as reuseable components that can be added to the toolbox of visual studio, so they typically are not specific to your business or code.

Here is a link that goes into the different forms with some code examples: http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsCreatingControls.aspx

Solution 2

You may want to use UserControl if you want to group many controls in a group. That is, create a group of controls. This is tipically done when you want to use same group on control in different part within your project.

You may want to use the CustomControl when you want to extend an existing control. The control is compiled in a DLL file, which you can reference from different projects.

Share:
15,218
sree aneev
Author by

sree aneev

Updated on June 11, 2022

Comments

  • sree aneev
    sree aneev almost 2 years

    What is the difference between UserControl and CustomControl in C# using WindowsForm?

  • Kenneth K.
    Kenneth K. about 11 years
    "Custom controls don't a visual interface." Is this statement accurate? I thought the "custom" in CustomControl meant that you (the developer) were drawing the visual aspects of the control (e.g. setting bounds, client area, shape, etc.). I thought that in a CustomControl you spend much more time involved with the OnPaint method and the like.
  • ken2k
    ken2k about 11 years
    -1, you're talking about ASP.Net, OP asked about winforms.
  • Scott S
    Scott S about 11 years
    @ken2k - apologies, I have updated my answer to cover Windows Forms too.
  • ken2k
    ken2k about 11 years
    @Digger I removed the downvote as you edited, but maybe you could remove the ASP.Net part as this specific question is not about ASP.Net?
  • Scott S
    Scott S about 11 years
    @ken2k - I have now removed the asp.net bit. I misread the question as I am trying to do many things at once. Like my job and surfing stack overflow. Hope its ok now.