Showing 2 secondary forms from the main form and then closing both forms will cause the main form to lose focus. (another application gets activated instead of mine)
The secondary forms are created either by the Main Form directly or by creating the third form from the second form.
The secondary forms set caFree in the OnClose event:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin Action := caFree; end;
Using Delphi 2009 (Update 3 & 4) with XP SP3.
Here are my steps for reproducing the problem:
- Create a new VCL forms applications
- Assign the OnClose event as above
- Drag a button onto the created form
- In the click handler create a new TForm1 and show it as below
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:
with TForm1.Create(Application) do show;
Is there any way to stop my main form from losing focus?
(Interestingly, when creating both secondary forms directly from the Main Form, the issue will only appear when closing the first created form then the second created form)
In the past I had the same issue which was solved by updating my delphi installation, but in that scenario I didn't use caFree in the OnClose event which is the cause for this bug.
A recommendation to set the Parent property on the secondary forms to Main Form, makes the new forms bounded to the Main Form which I'd rather not have. (and the solution proposed there to always reactivate the Main Form causes the activation order of the forms to be lost)