Last Updated:

TComponent Class | Delphi | Tutorial

The TComponent class is the ancestor of all VCL components. It is used as the basis for the creation of non-visual components and implements the basic mechanisms that ensure the functioning of any component. It displays the first properties that appear in the Object inspector. This property is:

property Name: TComponentName;

It contains the name of the component instance that is used to identify the component in the application.

Note
The TComponentName type is a regular string:

type TComponentName = type string;

Property Tag: Longint; is a helper property and does not affect the operation of the component. It is left to the developer, who can assign values to it at will. For example, you can use this property to identify components in an additional, more convenient way.

Components have their own hierarchy, so the class introduces a mechanism for accounting and managing the components for which this component is the owner. Properties and methods that are responsible for management are given in Table. 2.1.

Table 2.1. Properties and methods for managing the list of components.

Property (method)Description
property Components [Index: Integer]: TComponent;Contains an indexed list of pointers for all components for which the component is the owner)
property ComponentCount: Integer;Number of slave components
property Owner: TComponent;Specifies which component owns the given component
property Componentlndex: Integer;The index of this component in the owner list
procedure InsertComponent (AComponent: TComponent);Inserts an AComponent component into a list
procedure RemoveComponent (AComponent: TComponent);Removes an AComponent component from the list
procedure FindComponent (AName: string): TComponent;Searches for a component by name AName
procedure DestroyComponents;Designed to destroy all components subordinate to this

 

Very important property:

type TComponentState = set of (csLoading, csReading, csWriting, csDestroying,
 csDesigning, csAncestor, csllpdating, csFixups, csFreeNotification, cslnline, csDesignlnstance);
 property ComponentState: TComponentState;

Gives an idea of the current state of the component. In Table. Figure 2.2 describes the possible states of the component. The state can change as a result of the component receiving some message, developer actions, running a share, and so on. This property is actively used by the development environment.

Table 2.2. The possible states of the component.

ConditionDescription
csLoadingInstalled when a component is loaded from a thread
csReadingSet when reading property values from a stream
csWritingSet when property values are written to a stream
csDestroyingInstalled when a component is destroyed
csDesigningDevelopment status. Installed when working with a form at design time
csAncestorInstalled when a component is transferred to a form. to enter this state the csDesigning state must already be set
csUpdatingSet when property values change and the result is displayed on the owner form. to enter this state the csAncestor state must already be set
CsFixupsInstalled if the component is associated with a component of another form that is not already loaded into the development environment
csFreeNotificationif this state is set other components that are associated with the data are notified when the data is destroyed
cslnlineDefines the top-level component in the hierarchy. Used to identify the root object in expandable properties
csDesignlnstanceDefines the root component at design time

To ensure the operation of the mechanism of action (see Chapter 8), the method is designed:

function ExecuteAction(Action: TBasicAction): Boolean; dynamic;

It is called automatically when you want to execute the action that is intended for this component.

At the TComponent class level, the IUnknown and IDispatch COM interfaces are supported.

Through the property:

property ComObject: IUnknown;

You can enforce the methods of these interfaces.

Thus, the TComponent class has everything to use as an ancestor, to create its own non-visual components.