What is the purpose of custom controls in Visual C #?

User controls - are they for special purposes?

As far as I can tell, they are no different from forms - they have the same set of tools and functions.

Are there certain points when they are suitable for use over forms? It would be interesting to understand why they are good.

+6
c # user-controls
source share
6 answers

You use them to group a set of controls and behavior together in a reusable way. You cannot show the control on screen unless it is added to the form somewhere.

One good example is a text box. Very often there is a shortcut next to your text fields. You can create a custom control to make it easier. Just remove the shortcut and text field on the control, show all your properties, configure a new control on the toolbar, and now you can simply drop this control on your form, and not place the shortcut and toolbar on the form separately.

You might think of them as a panel that โ€œremembersโ€ what controls you. And one more important thing. You can also put code in these controls and use it to create special actions in your custom controls.

+10
source share

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

All controls can be reused. Almost all controls are reused in one form / window / panel / etc. For example, a TextBox is a control.

There are two ways to create your own reusable control:

  • User control
    • Fully custom and reusable.
    • Created completely in code.
    • You get more granular control over what makes your control this way.
    • Lighter weight (usually) because there is nothing added in Visual Studio for convenience.
    • ASP.Net only: There is no โ€œHTMLโ€ file for use or editing.
  • User control
    • Fully custom and reusable.
    • Created partly in the designer in Visual Studio and partly in code. (through the code behind)
    • The visual aspect is much easier to handle.
    • A bit heavier as there is already existing code added by the framework to support design within Visual Studio.
    • Only in ASP.Net: you can change the appearance a bit by editing the .ascx file (mostly HTML).
+9
source share

User controls are used to reuse controls. Imagine you need a search box on several pages of your application. You can create a search user control and drop it on every page where you want it to be visible.

So, this is nothing more than a container that combines reusable blocks for your pages.

+7
source share

The forms have a lot of extra furniture that you donโ€™t need if you just want to put together a collection of controls - for example, minimize and maximize buttons. If you just simply grouped your controls in a panel, you will have all the event handlers in the same form as the panel - using a user control, the event handling code is in the user control class, and not in the form class.

+1
source share

You can reuse the same control in many forms. In fact, all the elements that you use when creating windows are controls. User controls are simply additional controls that extend the library of controls provided by .NET.

0
source share

In ASP.NET, user controls let you split your page into reusable components. For example, you might have a search box that can be used in different places on your website, so you should use a user control. They can also be used to partially cache pages . You can cache parts of your page to improve performance.

0
source share

All Articles