Make a component execute a procedure every time the main form is activated (Views: 101)


I want my component to execute my procedure every time the main form gets activated. I don't want the main form to execute an event. The component should do it. It's a component I use in every application I write, and the procedure always does the same thing. So I just want to avoid adding the same routine over and over again in the main form. Even when it's just putting a TComponent.Execute in Form1.FormActivate. Is it possible?


It can be non-visual component as well in that case. You can simply drop on a form the following component. In TActivateHook.FormActivate you put the code for all forms, if you want any special processing for particular form, you put that code into that form's OnActivate.

{ ... }
  TActivateHook = class(TComponent)
    OldActivate: TNotifyEvent;
    procedure FormActivate(Sender: TObject);
    procedure Loaded; override;

  { TActivateHook }

procedure TActivateHook.FormActivate(Sender: TObject);
  if Assigned(OldActivate) then
  {Put your code here}

procedure TActivateHook.Loaded;
  if not (csDesigning in ComponentState) and (Owner <> nil) and
    Owner.InheritsFrom(TForm) then
    OldActivate := TForm(Owner).OnActivate;
    TForm(Owner).OnActivate := FormActivate;

<< Back to main page