Creating a custom component is the ultimate step in achieving code reuse in Delphi. A custom component is a descendant of an existing component class (usually TComponent or a visual descendant like TEdit) that encapsulates custom properties, methods, and visual behavior.
1. Component Prerequisites
A custom component requires two main parts:
- The Component Unit (
.pas): The code that defines the component’s class, logic, properties, and methods. - The Design Package (
.dpk): A special package file used by the Delphi IDE to register the component so it appears in the Component Palette.
2. The Custom Component Class Structure
All installable components must descend from TComponent or a component that descends from it (like TCustomEdit). Using TCustom... classes (e.g., TCustomEdit) gives you control over the behavior without having to manage all the visual aspects of the base control.
A. Key Methods to Override
| Method | Purpose |
constructor Create(AOwner: TComponent); override; | The constructor must accept an AOwner parameter. This establishes the component’s place in the component hierarchy, allowing the IDE to manage its lifecycle. |
procedure Loaded; override; | Called when the component is loaded from the form file (.dfm). Use this to finalize initialization after all properties have been loaded. |
procedure Paint; override; | (For visual components only) Contains the drawing code to render the component on the form. |
B. The Component Constructor
The Create constructor must call the ancestor’s constructor, passing the AOwner.
// In the implementation section of the component unit
constructor TMyValidatedEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner); // Calls ancestor constructor (TCustomEdit)
// Set default properties at design time
fRequired := False;
Color := clWhite;
end;
3. Implementing Properties
Custom components typically expose custom functionality via published properties, which makes them visible in the Object Inspector (as covered in D1.7).
- Field Storage: The actual value is stored in a private field (e.g.,
fRequired). - Accessor Methods: Custom properties often use read/write methods to control value assignment.
// Example: Creating a 'Required' property
type
TMyValidatedEdit = class(TEdit) // Inherits from TEdit
private
fRequired: Boolean;
procedure SetRequired(const Value: Boolean);
published // Makes the property visible in the Object Inspector
// Note the 'read fRequired' for direct access and 'write SetRequired' for method-controlled assignment
property Required: Boolean read fRequired write SetRequired default False;
end;
// Implementation of the SetRequired procedure
procedure TMyValidatedEdit.SetRequired(const Value: Boolean);
begin
if fRequired <> Value then
begin
fRequired := Value;
// Optional: Redraw the control if the change affects its appearance
// Invalidate;
end;
end;
4. Component Registration and Installation
To make the component available in the IDE, it must be registered within a Design-Time Package (.dpk).
A. The Registration Procedure
In your component unit, you must have a procedure that registers the component. This procedure is called automatically when the package is installed.
// At the end of the component unit's implementation section
initialization // Automatically executes when the unit is loaded
// RegisterClass is for non-component classes
// RegisterComponents is for components placed on the palette
RegisterComponents('My Custom Tab', [TMyValidatedEdit]);
end.
'My Custom Tab': This is the name of the new tab that will appear in the Component Palette.[TMyValidatedEdit]: A list of component classes (separated by commas) to place on that tab.
B. Creating and Installing the Package
- File $\rightarrow$ New $\rightarrow$ Package – Delphi. This creates a new
.dpkfile. - Add Unit: Right-click the package in the Project Manager, select Add…, and add your component’s
.pasunit file. - Compile: Right-click the package and select Compile.
- Install: Right-click the package and select Install.
Once installed, the IDE registers the component, and it will appear in the specified tab on the Component Palette, ready for drag-and-drop use in any new VCL or FMX project.
