Delphi disable form while loading

Andrey picture Andrey · Jul 6, 2013 · Viewed 7.4k times · Source

In my application, I have a main form, with the ability to load some images in database. While images is loading, I want to show a form, with the progress indicator (with bsNone border style).

But, if I show with form with ShowModal, execution of main form is stopped, so I can't to that.

If I call Show, user have access to all other form components, and it can be dangerous, while photo is not loaded completely.

I need to get the way, to disable everything on main form, while loading isn't completed.

Please, advice me, how it is possible.

Answer

Remy Lebeau picture Remy Lebeau · Jul 6, 2013

Set the MainForm as the PopupParent for the progress form so that the MainForm can never appear on top of the progress form. Then simply set MainForm.Enabled := False while the progress form is open and set MainForm.Enabled := True when the progress form is closed.

procedure TMainForm.ShowProgressForm;
begin
  with TProgressForm.Create(nil) do
  begin
    PopupParent := Self;
    OnClose := ProgressFormClose;
    Show;
  end;
  Enabled := False;
end;

procedure TMainForm.ProgressFormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
  Enabled := True;
end;

This simulates howShowModal() behaves to the user (the MainForm is not user-interactive while the progress form is open), but without blocking the code.