Search code examples
delphidelphi-xe

How can I make a a dialog box happen directly after my app's main form is visible?


I have been using TForm's OnActivate event to give me the opportunity to show a dialog box as soon as my app has started. I want the main form to already be loaded & visible. What's a good way to do this?

I have found that OnActivate works fine unless the forms WindowState is wsMaximized.

in the past I've accomplished what I want in various ways but I expect there is a better way.

Here's what worked for me:

procedure TForm1.FormCreate(Sender: TObject);  
begin 
  Application.OnIdle:=OnIdle; 
end; 

procedure TForm1.OnIdle(Sender: TObject; var Done: Boolean); 
begin 
  Application.OnIdle:=nil; 
  form2:=TForm2.Create(Application); 
  form2.ShowModal; 
end;

Is there a better way?


Solution

  • In the MainForm's OnShow event, you can do one of the following to show the dialog box after a delay that allows the MainForm to finish fully showing itself first:

    • start a short timer.
    • PostMessage() a custom window message to yourself.
    • use TThread.CreateAnonymousThread() or TTask to call TThread.Queue().
    • use TThread.ForceQueue() (10.2 Tokyo and later only).