title: How to prevent from loss of focus when  showing and/or closing secondary forms from the main form?
body: <p>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)</p>
  
  <p>The secondary forms are created either by the Main Form directly or by creating the third form from the second form.</p>
  
  <p>The secondary forms set caFree in the OnClose event:</p>
  
  <pre>
  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  begin
    Action := caFree;
  end;
  </pre>
  
  <p>Using Delphi 2009 (Update 3 &amp; 4) with XP SP3.</p>
  
  <p>Here are my steps for reproducing the problem:</p>
  
  <ol>
  <li>Create a new VCL forms applications</li>
  <li>Assign the OnClose event as above</li>
  <li>Drag a button onto the created form</li>
  <li>In the click handler create a new TForm1 and show it as below</li>
  </ol>
  
  <p>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.</p>
  
  <p>This is my code in the button click event handler:</p>
  
  <pre>
  with TForm1.Create(Application) do
      show;
  </pre>
  
  <p>Is there any way to stop my main form from losing focus?</p>
  
  <p>(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)</p>
  
  <hr>
  
  <p>In the past I had <a href="https://stackoverflow.com/questions/1897181/closing-a-secondary-delphi-form-causes-the-main-form-to-lose-focus">the same issue</a> 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.</p>
  
  <p><a href="http://www.delphigroups.info/3/6/168065.html" rel="nofollow noreferrer">A recommendation</a> 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)</p>