共用方式為


Creating and Using Components in Visual Basic

A component is a class that implements the System.ComponentModel.IComponent interface or that derives directly or indirectly from a class that implements IComponent. A .NET Framework component is an object that is reusable, can interact with other objects, and provides control over external resources and design-time support.

An important feature of components is that they are designable, which means that a class that is a component can be used in the Visual Studio Integrated Development Environment. A component can be added to the Toolbox, dragged and dropped onto a form, and manipulated on a design surface. Notice that base design-time support for components is built into the .NET Framework; a component developer does not have to do any additional work to take advantage of the base design-time functionality.

A control is similar to a component, as both are designable. However, a control provides a user interface, while a component does not. A control must derive from one of the base control classes: Control or Control.

When to Create a Component

If your class will be used on a design surface (such as the Windows Forms or Web Forms Designer) but has no user interface, it should be a component and implement IComponent, or derive from a class that directly or indirectly implements IComponent.

The Component and MarshalByValueComponent classes are base implementations of the IComponent interface. The main difference between these classes is that the Component class is marshaled by reference, while IComponent is marshaled by value. The following list provides broad guidelines for implementers.

  • If your component needs to be marshaled by reference, derive from Component.

  • If your component needs to be marshaled by value, derive from MarshalByValueComponent.

  • If your component cannot derive from one of the base implementations due to single inheritance, implement IComponent.

For more information about design-time support, see Design-Time Attributes for Components and Extending Design-Time Support.

Component Classes

The System.ComponentModel namespace provides classes that are used to implement the run-time and design-time behavior of components and controls. This namespace includes the base classes and interfaces for implementing attributes and type converters, binding to data sources, and licensing components.

The core component classes are:

Some of the classes used for component licensing are:

Classes commonly used for describing and persisting components.

  • TypeDescriptor. Provides information about the characteristics for a component, such as its attributes, properties, and events.

  • EventDescriptor. Provides information about an event.

  • PropertyDescriptor. Provides information about a property.

See Also

Tasks

How to: Access Design-Time Support in Windows Forms

How to: Extend the Appearance and Behavior of Controls in Design Mode

How to: Perform Custom Initialization for Controls in Design Mode