Link to home
Start Free TrialLog in
Avatar of Jim Culpepper
Jim Culpepper

asked on

activating inactive application without changing focus

I have an application with a form that contains quite a few edit boxes.  Suppose EditBoxA has the focus and I switch to another application without ocluding my application window.  Now, if I return to my application by clicking on the form and I happen to hit another Edit Box, say EditBoxB,, my application becomes active and the focus has now changed to EditBoxB.  Microsoft application such as Excel do not behave like this.  How can I get my application to become active without changing the focus no matter where the user might click on the form to make the application active?
Avatar of _Katka_
_Katka_
Flag of Czechia image

Hi, you may save the active component
on Form.OnDeactivate and restore it by
SetFocus on Form.OnActivate. Just like:

var
  Saved:TWinControl;

procedure Form1.FormDeactivate(Sender:TObject);
begin
  Saved:=ActiveControl;
end;

procedure Form1.FormActivate(Sender:TObject);
begin
  if Assigned(Saved) then Saved.SetFocus;
end;

regards,
Kate
 
ASKER CERTIFIED SOLUTION
Avatar of _Katka_
_Katka_
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Culpepper
Jim Culpepper

ASKER

Trapping the event on the form didn't seem to work.  However, adding an Application Events component to the form such as the following worked great:

procedure TMainForm.ApplicationEvents1Activate(Sender: TObject);
begin
  if    Assigned(SavedActiveComponent)
  then  SavedActiveComponent.SetFocus;
end;

procedure TMainForm.ApplicationEvents1Deactivate(Sender: TObject);
begin
  SavedActiveComponent := self.ActiveControl;
end;

Thanks for the insight.
You're welcome :)

Kate