Search code examples
formsdelphieventsmdi

Event when all mdi forms are closed


Guys, I'd like if anyone knows any event or method that I can intercept when all MDI forms were closed.

Example:

I want to implement an event in my main form where when I close all MDI forms, such an event was triggered.

Grateful if anyone can help.


Solution

  • MDI child forms (in fact any form), while being destroyed, will notify the main form. You can use this notification mechanism. Example:

    type
      TForm1 = class(TForm)
        ..
      protected
        procedure Notification(AComponent: TComponent; Operation: TOperation);
          override;
    
      ..
    
    procedure TForm1.Notification(AComponent: TComponent; Operation: TOperation);
    begin
      inherited;
      if (Operation = opRemove) and (AComponent is TForm) and
          (TForm(AComponent).FormStyle = fsMDIChild) and
          (MDIChildCount = 0) then begin
    
        // do work
    
      end;
    end;