-1

The problem is: How to hide additional form when application minimizes because after application restores, additional form can't be closed. Attached code shows the behavior. First I open additional form by pressing button. It has set Form Style set fsStayOnTop. Then I press timer button and minimize main form. After timer restores forms the additional one can't be closed.

program MINIBUG;

uses
  Vcl.Forms,
  MainForm in 'MainForm.pas' {Form7},
  AddForm in 'AddForm.pas' {Form8};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm7, Form7);
  Application.CreateForm(TForm8, Form8);
  Application.Run;
end.

unit AddForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm8 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

end.

unit MainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AddForm, Vcl.ExtCtrls;

type
  TForm7 = class(TForm)
    btnAddForm: TButton;
    tmr1: TTimer;
    Button1: TButton;
    procedure btnAddFormClick(Sender: TObject);
    procedure tmr1Timer(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form7: TForm7;


implementation

{$R *.dfm}

procedure TForm7.btnAddFormClick(Sender: TObject);
begin
  Form8.Show;
end;

procedure TForm7.Button1Click(Sender: TObject);
begin
  tmr1.Enabled := True;
end;

procedure TForm7.tmr1Timer(Sender: TObject);
begin
  tmr1.Enabled := False;
  form8.Close;
  Application.Restore;
end;

end.
Dohn Joe
  • 11
  • 2
  • 1
    I'm afraid that you are not providing us with enough needed information to help you solve your problem. The most crucial information that you left out is the code that executes after you double-click on the file that you application is set to open. I'm guessing that the problem might actually lie in this code. – SilverWarior Sep 05 '17 at 15:09
  • 2
    Provide a [mcve] – David Heffernan Sep 05 '17 at 15:14
  • I can now reproduce the problem you describe with the presented code. I understand that this is just mimicking the real situation. So please explain what real world action the `form8.Close;` in the `OnTimer` event is replicating. If that line is outcommented there's no problem. – Tom Brunberg Sep 06 '17 at 17:06
  • I have files associated with program so I can open file by doubleclick. When application runs normally, opening file does not pose problem. When I minimize app with visible Form8 and then open file by doubleclick, Form8 also appears no matter its Visible flag setting. And it can't be closed. So I need a solution to make Form8 perform properly or to hide it (or close) on application restoration. – Dohn Joe Sep 07 '17 at 11:28
  • I have to mention that application is singleton, so doubleclick on file won't create its new instance. – Dohn Joe Sep 08 '17 at 07:47
  • If you address your comment ( like @DohnJoe ) the adressee will be notified about pending messages. Now I was unaware of your response to my earlier comment and found it only by luck when I reviewed my latest activities. – Tom Brunberg Sep 10 '17 at 07:46

1 Answers1

0

Testcase error?

I'm not sure your test case is correct. If the timer event simulates the act of doubleclicking an associated file, why would that lead to a Form8.Close action? You said, a part of the problem was that the additional form became visible (together with the main form) when the associated file was opened, so the hiding (Form.Hide) should take place when you start the timer, and at OnTimer the form should be shown (`Form.Show).

Answer

Anyway, the answer to your actual question, How to hide additional form when application minimizes is that you don't have to do anything special. The additional form will be hidden too, without any actions.

If you for some reason want to or must actively hide the additional form, do so by adding a TApplicationEvents component to your main form, and use its OnMinimize event to call Form8.Hide and OnRestore event to call Form8.Show.

Also consider

Btw, there is a difference if you choose Form.Close or Form.Hide. Close goes through a procedure to call CloseQuery() while Hide simply sets the Visible property yo False.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54