简体   繁体   English

Visual C#中用户控件的用途是什么?

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

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. 了解它们有什么好处会很有趣。

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. 您也可以将代码放在这些控件中,并使用它们在自定义控件中构建特殊行为。

I have to disagree (slightly) with the selected answer. 我必须(略微)不同意所选答案。 Reusability is only part of what a UserControl is for. 可重用性只是UserControl的一部分。

All Controls are reusable. 所有控件都可重复使用。 Almost all controls are reusable on the same Form/Window/Panel/etc. 几乎所有控件都可以在同一个窗体/窗口/面板/等上重复使用。 For example, a TextBox is a control. 例如,TextBox是一个控件。

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. 更轻(通常),因为在Visual Studio中没有为可设计性添加任何内容。
    • In ASP.Net only: No "HTML" type file to use or edit. 仅在ASP.Net中:不使用或编辑“HTML”类型文件。
  2. User Control 用户控制
    • Completely custom, and reusable. 完全定制,可重复使用。
    • Created partially in a designer in Visual Studio, and partially in code. 部分在Visual Studio中的设计器中创建,部分在代码中创建。 (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. 稍微重一点,因为框架中添加了预先存在的代码以支持Visual Studio内部的设计。
    • In ASP.Net only: You can change the appearance a bit simply by editing the .ascx file (basically HTML). 仅在ASP.Net中:您可以通过编辑.ascx文件(基本上是HTML)来稍微改变外观。

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. 因此,它只不过是一个聚合页面可重用块的容器。

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. 如果您只是简单地将控件分组到一个Panel中,那么您将拥有与面板相同的所有事件处理程序 - 使用用户控件,事件处理代码位于用户控件类中,而不是表单类。

You can reuse the same control on many forms. 您可以在许多表单上重用相同的控件。 In fact all items you are using while creating windows forms are the controls. 事实上,您在创建Windows窗体时使用的所有项目都是控件。 User controls are just extra controls extending controls library provided by .NET. 用户控件只是扩展.NET提供的控件库的额外控件。

In ASP.NET, user controls enable you to split your page into reusable components. 在ASP.NET中,用户控件使您可以将页面拆分为可重用的组件。 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. 您可以缓存部分页面以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM