2007. április 26., csütörtök

Disable the main form while a dialog box is shown


Problem/Question/Abstract:

I'm trying to set up a "Please Wait" box. I want it to be modal in the sense that my main form is deactivated while I have this box showing. But, in the function that displays the "Please Wait" box, I want the code to continue rather than stall, waiting for the box to close.

Answer:

{ ... }
WaitBox.Show; {shows your WaitBox}
Enabled := false; {disables the whole main form (Self.Enabled)}
Application.ProcessMessages; {let the two forms update themselves}
try
  { ... Do next steps }
finally
  Enabled := true;
  WaitBox.Hide;
end;

Note that the WaitBox must be visible before you can disable the main form. You needn't disable each single component. With this construction you can easily add a Cancel-Button to your WaitBox. Set a public property (CancelPressed) to TRUE if the Cancel Button is pressed and you can do something like this:

{ ... }
repeat
  { next steps }
  Application.ProcessMessages;
until
WaitBox.CancelPressed
{ ... }

Nincsenek megjegyzések:

Megjegyzés küldése