delphieventsformsshowdelphi-2006

Where should I put code to execute once after my Delphi app has finished initialising?


I have functions I want to perform after my app has finished initialising and the main form has been created. I did have the code (call it ProcedureX) in the forms OnShow event, but I have just noticed that it is being called twice, because OnShow is firing twice. It fires when the main program DPR calls:

Application.CreateForm(TMainForm, MainForm) ;  

as I would expect. But after that, when I read stuff from an INI file that includes the forms on-screen position, I have a call:

MainForm.position := poScreenCenter ;

This, it would appear fires the OnShow event again.

Where can I put my call to ProcedureX, which must only be called once, and which needs the main form to be created before it can execute?


Solution

  • You can test and set a flag once you call the procedure for the first time. Like so:

    type
      TForm1 = class(TForm)
        procedure FormShow(Sender: TObject);
      private
        FRunOnce: Boolean;
      public
        [...]
    
    [...]
    
    procedure TForm1.FormShow(Sender: TObject);
    begin
      if not FRunOnce then begin
        FRunOnce := True;
        ProcedureX;
      end;
    end;