I just added the following OnClose handler to my application's main form:

I just added the following OnClose handler to my application's main form:

procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Action := caMinimize;
end;

But when I press the close button, the form does not minimize but the application terminates.

WTF?

Here is the code from VCL.Forms that causes this:

procedure TCustomForm.Close;
var
    CloseAction: TCloseAction;
begin
  // ...  
  DoClose(CloseAction);
  if CloseAction <> caNone then
    if Application.MainForm = Self then
      Application.Terminate
  // ...    
end;

When was this introduced? Or was it always the case that no matter what CloseAction you return from OnFormClose of your main form, if it's not caNone, the application terminates?

Comments

  1. It's always been this way. When the main form is closed the application terminates.

    ReplyDelete
  2. unless there's something I'm missing, you first set:

     Action := caMinimize;

    and then:

    if CloseAction <> caNone then -- CloseAction = caMinimize at this point

    not sure what I'm missing...

    ReplyDelete
  3. It has been like this at least since Delphi 6. The TCustomForm.Close method hasn't changed at all. I think it strange too, that minimizing the main form should result in terminating the application, but that has been the case since about forever, apparently.

    ReplyDelete
  4. Dorin Duminica The second part is not Thomas' code but from the VCL.

    ReplyDelete
  5. Kenneth Cochran But I am not closing it but minimizing. That's what setting CloseAction to caMinimize is supposed to do according to the online help. So, in order to get the desired behaviour, I have to change the code to:

    procedure TMyForm.FormClose(
      Sender: TObject; var Action: TCloseAction);
    begin
      Action := caNone;
      WindowState := wsMinimized;
    end;

    Which is plain annoying.

    ReplyDelete
  6. Daniela Osterhagen thank you I knew I was missing something, too tired >.<

    ReplyDelete
  7. Thomas Mueller stop whining and get over it. You have got a solution, move on!

    ;-)

    ReplyDelete
  8. Daniela Osterhagen always the vanguard of courteousness, aren't we? ;-)
    But you are right, of course.

    ReplyDelete
  9. Daniela Osterhagen A nice, light touch ;)

    ReplyDelete
  10. BTW, how is the application supposed to be closed? For another solution you can use the CloseQuery event, set CanClose to false and call Application.Minimize.

    ReplyDelete
  11. This is what I ended up with:

    procedure TMyForm.FormClose(
    Sender: TObject; var Action: TCloseAction);
    begin
      if CheckSomeCondition then begin
        Action := caNone;
        Hide;
      end;
    end;

    ReplyDelete
  12. I agree completely with the VCL implementation.

    You can't have an application that won't close in any way except Task Manager.

    I think that is bordering on un-ethical and it certainly is un-friendly.

    Kind Regards,

    A

    ReplyDelete
  13. What about a tray icon application that closes through the exit entry in the popup menu?

    ReplyDelete
  14. That shouldn't be on the X, should be on the minimize.

    I hate applications that don't respect that kind of contract.

    If I want to close it, then let me close it, don't oblige me to fumble across a gazillion icons to find the right one.
    At the very least,give me an option to use the default behaviour and make sure it's the default.

    A

    ReplyDelete
  15. Thomas Mueller  Be careful when doing stuff like this, or you can cause the system to hang at shutdown, with your app refusing to close. Better make sure you handle those EndSession messages properly.

    Andrea Raimondi I have never had a single user complaint about my app that minimizes to the taskbar and closes to the tray, with an option on the tray icon's popup menu to exit. They actually appreciate the way I designed it to work. While I don't agree that all apps should work this way, there are cases where this is a good design decision. Those cases are few and far between.

    ReplyDelete
  16. I hate the applications that do that, unless there's a good reason.
    Very often, however, that is not the case.

    I have seen this happen * so many times* I am totally annoyed by whoever does this.

    Please don't do this.

    A

    ReplyDelete

Post a Comment