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

白昼怎懂夜的黑 提交于 2019-12-30 08:22:10

问题


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.


回答1:


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.




回答2:


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).



回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/1379493/what-are-the-purpose-of-user-controls-in-visual-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!