What is the difference between User Control, Custom Control and Component?

Svish picture Svish · Aug 24, 2009 · Viewed 86.2k times · Source

These are three different things you can add to a project and I am not quite sure if I understand the difference. They all seem to for example show up in the component toolbox when working with a Form. What are some common usage scenarios for each of them? What is the difference?

Answer

Fredrik Mörk picture Fredrik Mörk · Aug 24, 2009

The main difference between User Control, Custom Control and Component is that they inherit from different levels in the inheritance tree:

MyComponent
   |-> Component

MyCustomControl
   |-> Control
          |-> Component

MyUserControl
   |-> ContainerControl
          |-> ScrollableControl
                 |-> Control
                        |-> Component

So, in short you get a different amount of pre-wired functionality with the different options.

When would you use the different options? (these are thoughts and opinions, not truths)

  • Create a component if you want to provide functionality without UI (such as Timer components, data sources, ...)
  • Create a custom control if you want to make a component where you have full control over its visual appearance, and you don't want any baggage of unnecessary functionality. Typical cases would be simple controls with limited functionality (such as a button)
  • Create a user control if you are going to combine existing controls into reusable building blocks (such as two lists with buttons where you can move items between the lists).