When to add a Component Class vs User Control?

后端 未结 4 1482
面向向阳花
面向向阳花 2021-02-04 00:00

I have a general idea, and there are some obvious cases, but there are also some gray areas for me - when is it best to use to extend from a component and when is it best to cre

4条回答
  •  [愿得一人]
    2021-02-04 00:42

    There is one significant difference between a Component and a Control: Controls have user interface. All controls are also components, but not all components are controls. If you need to display a user interface, deriving from some kind of Control base (Control, UserControl, Form, etc.) would usually be required. If you just have behavior, such as with the BackgroundWorker component, then you would only need to derive from Component directly.

    Another note...both Components and Controls may be dropped onto a design surface. Components show up as an icon and a label in a special area, controls appear directly on the design surface. However, there is a third thing that you may use: a simple Class. If you do not need design surface support, I would recommend using a simple class rather than Component or Control. They are lighter in weight and less bloated when all you need is 100% pure behavior with no design-time support.

提交回复
热议问题