Mega Code Archive
How to aggregate two visual vcl components
Title: How to aggregate two visual vcl components
Question: An example that shows how to embed one control in another.
Answer:
Component writing is one of the strength of Delphi. This article shows one possible start !
TTestEdit is a TEdit descent that has a button inside. A click on this button sets the text property to the actual date. Important is the differenz between the owner and the parent in the vcl hierarchy.
unit TestEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TTestEdit = class(TEdit)
private
FButton: TButton;
protected
procedure ButtonClick(Sender: TObject);
public
constructor Create( AOwner: TComponent ); override;
published
end;
procedure Register;
implementation
procedure TTestEdit.ButtonClick(Sender: TObject);
begin
Text := DateToStr(Now);
end;
constructor TTestEdit.Create( AOwner: TComponent );
begin
inherited Create(AOwner);
Text := DateToStr(Now);
FButton := TButton.Create( self );
FButton.Visible := true;
FButton.Parent := self;
FButton.Height := Height-2;
FButton.Width := Height-2;
FButton.Left := Width - Height - 2;
FButton.top := top;
FButton.OnClick := ButtonClick;
end;
procedure Register;
begin
RegisterComponents('Standard', [TTestEdit]);
end;
end.
Another possible approach would be to inherit from TWinControl and embed a TEdit and a TButton. Than all properties that are acting like an edit-control must be defined again...