C# 面板和 UserControl 之间的区别
有人可以告诉我使用表单、面板或用户控件之间的区别吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有人可以告诉我使用表单、面板或用户控件之间的区别吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
窗体是一个控件和其他控件的容器。窗体是 Windows 应用程序的基本单元。
面板是一个控件和其他控件的容器。
用户控件是用户定义的控件。
请参阅:
A form is a control and a container for other controls. A form is the base unit of a windows application.
A panel is a control and a container for other controls.
A usercontrol is a user defined control.
See:
根据 MSDN 面板类 是“用于对控件集合进行分组”,而 用户控件 “提供一个空控件,可用于创建其他控件”。
你是对的:这对你决定是否应该使用面板或用户控件没有太大帮助。
区别之一是 Panel 是 ScrollableControl,而 UserControl 是 ContainerControl(也是 ScrollableControl)。因此,如果您需要 ContainerControl 功能,请考虑使用 UserControl。
你可能不知道 ContainerControl 能做什么,所以你不能用 Panel 做什么,因此以下内容可能更有用:
在面向对象编程中,在 Winforms 中,每当你想要一个具有行为的类时与另一个类类似,但仅略有不同,您认为是从另一个类派生的。
因此,如果您想要一个按钮在按下时改变颜色,并在再次按下时返回到其原始颜色(如开关按钮),您可能会考虑从类 Button 派生,或者可能从类 CheckBox-in-the- 派生按钮形状。
通过将其设为单独的类,您可以在类似情况下重用代码。每当您只使用它一次时,通常我们不会费心将其设为特殊类。我们通常不会为“我的表单中的选择按钮,单击时...”创建一个特殊的类,但如果您将以十种不同的形式使用此按钮,那么创建一个 SelectButton 类可能是更明智的选择。
类似地,如果您有一组具有某些行为的控件,并且您计划以不同的形式使用它们,请考虑创建一个用户控件,在其中放置此行为。好处是此行为的代码隐藏在控件内。 UserControl 的用户只需知道它的作用,而不需要知道它是如何完成的。您甚至可能想隐藏这是如何完成的,因此用户(=代码,而不是操作员)无法访问它
面板或多或少像没有周围矩形的GroupBox:如果您想使用它而不是用户控件,请考虑使用它仅在此表单内使用它。类似于您如何使用“单击时...的按钮”:因为您仅在此处使用它,所以您不会从中派生。
我很少使用面板。派生类:TabPage、SplitterPanel...更有可能仅以这种形式使用。
每当我需要多个控件的组合时,特别是当它们相互交互时。例如,如果您有一个文本框和一个描述文本框中内容的标签,以及一个处理文本框中文本的“确定”按钮。在这种情况下,我通常将其设为用户控件。
我本可以从面板派生并添加标签、文本框和按钮,但用户可能会通过添加其他项目或调用会扰乱我的功能的面板函数来搞乱我的面板。
想一想:使用从Panel派生的类与使用UserControl类似于派生与聚合/组合:如果聚合,则可以限制对功能的访问,如果派生,用户可以访问所有父功能。
因此,如果您只想要有限的功能:显示/不显示,也许大小和位置,背景,但仅此而已:考虑创建一个 UserControl。如果您希望能够更改行为,请考虑使用面板,特别是如果您仅以一种形式使用它。
According to MSDN the Panel class is "Used to group collections of controls", while the User Control "Provides an empty control that can be used to create other controls".
You are right: this doesn't help you a lot to decide whether you should use a Panel or a User Control.
One of the differences is that a Panel is a ScrollableControl, while a UserControl is a ContainerControl (which is also a ScrollableControl). So if you want ContainerControl functionality, consider to use a UserControl.
You'll probably don't know what a ContainerControl does, so what you can't do with a Panel, hence the following might be more useful:
In object oriented programming, and so also in Winforms, whenever you want a class that behaves like another class, but only slightly different, you consider to derive from the other class.
So if you want a button that changes color when pressed, and returns to its original color when pressed again, (like an on-off button), you might consider to derive from class Button, or maybe from class CheckBox-in-the-shape-of-a-button.
By making it a separate class, you can reuse the code in similar situations. Whenever you will only use it once, then usually we won't bother to make it a special class. We will usually not make a special class for "The Select button in my form, which does ... when clicked", but if you will use this button in ten different forms, then it is probably wiser to create a SelectButton class.
Similar, if you have a group of controls, with some behaviour, and you plan to use that in different forms, consider to create a User Control, where you put this behaviour. The nice thing is that the code of this behaviour is hidden inside the control. Users of your UserControl only have to know what it does, not how this is done. You might even want to hide how this is done, so users (= code, not operators) can't access it
A panel is more or less like a GroupBox without a surrounding rectangle: consider to use it instead of a User Control if you will be using it only inside this Form. Similar to how you would us a "Button that does ... when clicked": because you use it only here, you don't derive from it.
I seldom use a Panel. The derived classes: TabPage, SplitterPanel, ... are more likely to be used only in this form.
Whenever I need combinations of several controls, especially if they interact with each other. For instance, if you have a text box and a label that describes what is in the textbox and an OK button that processes the text in the text box. In that case I usually make it a UserControl.
I could have derived from a Panel and add a Label, TextBox and Button, but then users could mess up with my Panel by adding other items, or calling Panel functions that would mess with my functionality.
Come to think of it: using a class derived from a Panel vs using a UserControl is similar to deriving vs aggregation / composition: If you aggregate, you can limit access to functionality, if you derive, users can access all parent functionality.
So if you only want limited functionality: show / no show, maybe size and position, background, but not much more: consider to create a UserControl. If you want the possibility to change the behaviour, consider to use a Panel, especially if you will use it in only one form.