views:

633

answers:

5

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:

  1. Create a new VCL forms applications
  2. Drag a button onto the created form
  3. In the click handler create a new TForm1 and show it

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?

+2  A: 

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...

François
Most important, the nil in the parameter sets the parent, so there is no parent, so it isn't a child form...
mj2008
@mj2008: No, it doesn't set the parent. The parameter to Create() is the *owner*, which makes it the one responsible for freeing the form. It has nothing to do with parentage.
Ken White
I tried it on more then one machine with the same results. What version of Delphi did you use? I can only reproduce it in Delphi 2009, not in Delphi 7
Y Low
François
I don't think I installed any Delphi updates. This is my exact version number 12.0.3170.16989I have this version on 3 machines with the same results. Is your version higher?
Y Low
Yep, higher: 12.0.3420.21218
François
I just upgraded to the latest version (12.0.3420.21218) and it's fixed.
Y Low
A: 

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.

Chris Becke
Note that the question is about closing non-modal child windows.
mghie
A: 

Try the following (and avoid the with):

with TForm1.Create(nil) do begin
  show;
  activate;
  bringtofront;
end;
Oliver Townshend
Does not change the results. Can you reproduce it on you machine?
Y Low
A: 

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

RR-NL
It's exactly like your code above! My version is 12.0.3170.16989Which version do you have?
Y Low
My version is 12.0.3420.21218.
RR-NL
Just a wild guess, but could you have some other application running which grabs the focus?
RR-NL
+3  A: 

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.

Y Low