2

This question already has an answer here:

What is the difference between UserControl and CustomControl in C# using WindowsForm?


2 답변


4

There are custom and user controls for both windows applications and web applications. The windows application controls have a .cs extenstion.

In a very general sense a user control is easier to create. You can drag existing controls like textboxes, labels, etc on to the form. Custom controls are generally more difficult (time consuming) to create, but offer greater flexibility, customiseability, and integration.

The major difference in a nutshell is this:

A user control is made up of existing controls. It is also sometimes referred to as a composite control because of this fact. A typical example is a login form. The form and all of the logic is contained within this 'reuseable' user control.

A custom control is a control that you create. In windows forms this means overriding the OnPaint method as in your example above. Custom controls do not have the same level of design time support as user controls do (ie dragging and dropping existing controls, etc). Custom controls are generally thought of as reuseable components that can be added to the toolbox of visual studio, so they typically are not specific to your business or code.

Here is a link that goes into the different forms with some code examples: http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsCreatingControls.aspx


  • "Custom controls don't a visual interface." Is this statement accurate? I thought the "custom" in CustomControl meant that you (the developer) were drawing the visual aspects of the control (e.g. setting bounds, client area, shape, etc.). I thought that in a CustomControl you spend much more time involved with the OnPaint method and the like. - Kenneth K.
  • -1, you're talking about ASP.Net, OP asked about winforms. - ken2k
  • @ken2k - apologies, I have updated my answer to cover Windows Forms too. - Scott Sellers
  • @Digger I removed the downvote as you edited, but maybe you could remove the ASP.Net part as this specific question is not about ASP.Net? - ken2k
  • @ken2k - I have now removed the asp.net bit. I misread the question as I am trying to do many things at once. Like my job and surfing stack overflow. Hope its ok now. - Scott Sellers

3

You may want to use UserControl if you want to group many controls in a group. That is, create a group of controls. This is tipically done when you want to use same group on control in different part within your project.

You may want to use the CustomControl when you want to extend an existing control. The control is compiled in a DLL file, which you can reference from different projects.

Linked


Latest