What are the purpose of User Controls in Visual C#?

c#, user-controls

Solution

You use them to group a set of controls and behaviors together in a re-usable way. You can't show a control on the screen unless it's added to a form somewhere.

One good example is a textbox. It's very common to have a label next to your textboxes. You can build a user control to make this easier. Just drop a label and a textbox on the control, expose whatever your properties you want, setup the new control in your toolbox, and now you can just drop this control on your form instead of needing to arrange a label and a toolbox on the form separately.

You could kind of think of them as a panel which "remembers" what controls you put on it. And there's one more important piece. You can put code in these controls as well, and use that to also build special behaviors into your custom controls.

Problem

User Controls -- Do they serve a special purpose? As far as I can tell they are no different to forms - they have the same toolbox and features. Are there certain times when they are appropriate to use over forms? It would be interesting to understand what they are good for.

Original source