Reputation: 9682
When showing a secondary form from the main form and from the second form showing a third form and then closing both forms will cause the main form to lose focus.
Using Delphi 2009 with XP SP3
Here are my steps for reproducing the problem:
Run the program. Click the button to show a second form. Click the button on the second form to create a third form. When closing both new forms the main form will lose its focus.
This is my code in the button click event handler:
// Using Self does not change the results with TForm1.Create(nil) do show;
Is there any way to stop my main form from losing focus?
Upvotes: 2
Views: 3246
Reputation: 9682
After upgrading my Delphi installation from version 12.0.3170.16989 (no updates) to version 12.0.3420.21218 (update 3 & 4) I could not reproduce the problem anymore.
Seems like it was a bug that was fixed in the update.
Upvotes: 3
Reputation: 186
Like François I cannot reproduce this behaviour with Delphi 2009 on Windows XP SP3. The form that was opened first gets the focus back as soon as the other forms are closed.
To be sure, is your project code:
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
And your Unit code:
unit Unit1;
interface
uses
Forms, Classes, Controls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
with TForm1.Create(nil) do
Show;
end;
end.
Corrected the name of François, sorry
Upvotes: 0
Reputation: 21640
I don't see how what you describe creates a "child" Form.
But anyway, I just tried with exactly what you described in your steps and could not reproduce it in D2009 (updates 3 & 4), whether I create the 2nd "child" from the main Form or from the 1st "child", and whatever the order in which I close them.
So, there must be something else you did not tell...
Upvotes: 2
Reputation: 153
Try the following (and avoid the with):
with TForm1.Create(nil) do begin
show;
activate;
bringtofront;
end;
Upvotes: 1
Reputation: 36026
From a pure Win32 perspective Applications tend to loose focus when popup windows are closed because the underlying framework has an order of operations issue. Windows will not activate a disabled window, so, when destroying a modal popup window it is very important that the parent window is (reenabled) BEFORE DestroyWindow is called on the popup.
I have no idea how this might apply to development in delphi or vcl. The code sample does not imply you have a lot - or any - control over how modal popup windows are destroyed.
Upvotes: 0