Title: Make your DB components more "Aware"
Question: Borland hides the DataLink field in the private section in his data aware components, so, if you want perform some action in response to dataset events, you must do it from the dataset not from the component itself.
With this tip, you can add data events to your DB derived components.
Answer:
This code makes a componente from TDBEdit. This new component has a event that fires when the dataset becomes active/desactive.
type
TMyDBEdit = class(TDBEdit)
private
FOnActiveChange: TNotifyEvent;
OldOnActiveChange: TNotifyEvent;
procedure InternalActiveChange(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
function GetDataLink: TFieldDataLink;
published
property OnActiveChange: TNotifyEvent
read FOnActiveChange
write FOnActiveChange;
end;
constructor TMyDBEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Save old event handler
OldOnActiveChange := GetDataLink.OnActiveChange;
GetDataLink.OnActiveChange := InternalActiveChange;
end;
function TMyDBEdit.GetDataLink: TFieldDataLink;
var
DataLink: TFieldDataLink;
begin
// This is the tip. This message gets a integer, that is to say
// a pointer to the private field FDataLink in the base class.
// CM_GETDATALINK constant is defined in Controls.pas
integer(DataLink) := perform(CM_GETDATALINK, 0, 0);
result := DataLink;
end;
procedure TMyDBEdit.InternalActiveChange(Sender: TObject);
begin
// Call the old event handler
if assigned(OldOnActiveChange) then OldOnActiveChange(Sender);
// Call our pretty event handler
if assigned(FOnActiveChange) then FOnActiveChange(self);
end;
Best regards.
Jorge.