Редагувати

Поділитися через


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 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. Base design-time support for components is built into .NET. 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 marshalled by reference, while IComponent is marshalled by value. The following list provides broad guidelines for implementers.

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

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

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

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.

Troubleshooting Control and Component Authoring
Explains how to fix common problems.

See also