?
Solved

activating inactive application without changing focus

Posted on 2005-02-28
4
Medium Priority
?
341 Views
Last Modified: 2012-05-05
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
Comment
Question by:wtfinc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 10

Expert Comment

by:_Katka_
ID: 13421963
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
 
LVL 10

Accepted Solution

by:
_Katka_ earned 1500 total points
ID: 13422061
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
 

Author Comment

by:wtfinc
ID: 13422110
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
 
LVL 10

Expert Comment

by:_Katka_
ID: 13422274
You're welcome :)

Kate
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question