Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

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?
0
wtfinc
Asked:
wtfinc
  • 3
1 Solution
 
_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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now