views:

337

answers:

6

Good afternoon all.

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.

Regards

+5  A: 

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 the "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.

Joel Coehoorn
So essentially like Panels but as a seperate entity?
Ric Coles
The key is that they are re-usable. You can put multiple instances of a control on a form.
Joel Coehoorn
A: 

You can reuse the same control on many forms. In fact all items you are using while creating windows forms are the controls. User controls are just extra controls extending controls library provided by .NET.

RaYell
+7  A: 

User Controls serve the purpose of reusing controls. Imagine you need a search box in several pages of your application. You can create a search user control and drop it in every page where you want it visible.

So, it's nothing more than a container that aggregates reusable blocks for your pages.

Dante
A: 

In ASP.NET, user controls enable you to split your page into reusable components. For example, you may want to have a search box which can be used in different places on your website, so you'd use a user control. They can also be used for partial page caching. You can cache portions of your page to improve performance.

Charlie
+1  A: 

Forms have a lot of extra furniture that you don't need if you simply want a collection of controls together - the minimize and maximize buttons for example. If you just simply grouped your controls in a Panel, you'd have all the event handlers on the same form as the panel - with a user control, the event handling code is in the user control class, not the form class.

Martin
+5  A: 

I have to disagree (slightly) with the selected answer. Reusability is only part of what a UserControl is for.

All Controls are reusable. Almost all controls are reusable on the same Form/Window/Panel/etc. For example, a TextBox is a control.

There are two ways to create your own reusable control:

  1. Custom Control
    • Completely custom, and reusable.
    • Created entirely in code.
    • You get a bit more granular control over what your control is doing this way.
    • Lighter weight (usually), because there isn't anything added in for designability within Visual Studio.
    • In ASP.Net only: No "HTML" type file to use or edit.
  2. User Control
    • Completely custom, and reusable.
    • Created partially in a designer in Visual Studio, and partially in code. (via code behind)
    • Much easier to deal with from a visual aspect.
    • A little heavier, as there is pre-existing code added in by the framework to support designing inside Visual Studio.
    • In ASP.Net only: You can change the appearance a bit simply by editing the .ascx file (basically HTML).
blesh