Make a component execute a procedure every time the main form is activated (Views: 711)
Problem/Question/Abstract: 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? Answer: 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. { ... } type TActivateHook = class(TComponent) private OldActivate: TNotifyEvent; procedure FormActivate(Sender: TObject); protected procedure Loaded; override; end; { TActivateHook } procedure TActivateHook.FormActivate(Sender: TObject); begin if Assigned(OldActivate) then OldActivate(Sender); {Put your code here} end; procedure TActivateHook.Loaded; begin inherited; if not (csDesigning in ComponentState) and (Owner <> nil) and Owner.InheritsFrom(TForm) then begin OldActivate := TForm(Owner).OnActivate; TForm(Owner).OnActivate := FormActivate; end; end; |