views:

509

answers:

4

I have a general idea, and there are some obvious cases, but there are also some gray areas for me - when is it best to use to extend from a component and when is it best to create a user control ? This pertains to a specific work problem I am trying to solve, but the specifics of that are not important - a general answer to this question is enough for me.

A: 

In general, I would use Component when the control does not have any user interface (or at least not one that is present on the form). If it is a UI control I would create a User Control instead.

Fredrik Mörk
+2  A: 

In WPF and Windows Forms, both, the main difference is that a UserControl is meant to be a collection of controls - a reusable, single object "composed" from multiple controls themselves.

You'd impelemnt a Component/CustomControl/Control instead of a UserControl if you are making a single, primitive control with new behavior, instead of making a "control" that's composed of smaller controls. Component usually is a non-visual behavior, where a CustomControl/Control is usually for a visual control.

Reed Copsey
A: 

I typically extend Control, or more ofter UserControl, only when I want to package some UI functionality. For Components, I think of the classic example, the Timer. It can be dropped onto the designer, configured through the Properties pane, and then accessed programmatically through the code behind. In short, I extend Component when I want to be able to manipulate some bundled-up state and behavior, an object with no UI, through the designer.

Travis Heseman
A: 

There is one significant difference between a Component and a Control: Controls have user interface. All controls are also components, but not all components are controls. If you need to display a user interface, deriving from some kind of Control base (Control, UserControl, Form, etc.) would usually be required. If you just have behavior, such as with the BackgroundWorker component, then you would only need to derive from Component directly.

Another note...both Components and Controls may be dropped onto a design surface. Components show up as an icon and a label in a special area, controls appear directly on the design surface. However, there is a third thing that you may use: a simple Class. If you do not need design surface support, I would recommend using a simple class rather than Component or Control. They are lighter in weight and less bloated when all you need is 100% pure behavior with no design-time support.

jrista