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?
wtfincAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_Katka_Commented:
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
 
0
_Katka_Commented:
Well that doesn't function as expected so try rather:

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    ..
    (your fields)
    ..
    procedure FormCreate(Sender: TObject);
  public
    FSaved:TWinControl;
    procedure Activate(Sender:TObject); // add these two procedures to override application events
    procedure Deactivate(Sender:TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Activate(Sender:TObject);
begin
  if Assigned(FSaved) then FSaved.SetFocus;
end;

procedure TForm1.Deactivate(Sender:TObject);
begin
  FSaved:=ActiveControl;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  inherited;
  Application.OnActivate:=Activate;
  Application.OnDeActivate:=Deactivate;
end;

regards,
Kate
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wtfincAuthor Commented:
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.
0
_Katka_Commented:
You're welcome :)

Kate
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.