Component vs. Control

2019-04-05 11:59发布

问题:

In the world of WinForms .Net controls

What is the difference between Component and Control?

  • Can a Control contains any Component?
  • Can a Component contains any Control?

回答1:

A Control has all the plumbing required to act as a window. The ability to respond to Window messages (WndProc) and having a Handle being foremost. Component is missing all that. It is really rather simple, it has design time support and it can be disposed, that's about it.

Components still can have a runtime representation, OpenFileDialog being the best example. But that is actually a dialog built into Windows, not Windows Forms.



回答2:

A Component is just a representation of an object/sub API. eg. ImageList is a component which is invisible and is just an API into a set of images. The fact you can drag and drop them onto forms is just sugar in the designer.

A Control is intended to be visible/interactable/nestable.



回答3:

In response to your comment, the Component and Control can be seen in the Object browser as being from different object hierarchies. A Component cannot be added within a Control, as the Controls collection only accepts objects with base type of Control.

A Component cannot contain Controls.



回答4:

A component is basically a class (it can be a dll, Activex Object).

  • It does not have User Interface capabilities that's why you cannot see a visual representation.
  • It is portable and not specific to any language.

While on the other hand control is component which provides User Interface capabilities.

  • Since control has UI, you can see its visual representation.
  • It is language specific and not portable.