views:

164

answers:

5

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?

+2  A: 

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;
Sertac Akyuz
@Sertac: I follow the convention "prefix fields with a F", so FRunOnce sounds more standard for me. Is not a rule, just a convention, like prefix class names with T. Just makes other's code easier to read.
jachguate
@jachguate - When there's the 'F' I somehow feel like it should have a getter/setter. But then that's my problem I guess... Edited the answer, thanks for pointing out.
Sertac Akyuz
OK, I had thought of doing that, but it seemed like treating the symptom, not the cause. I guess I was looking for some other event (like "OnEverythingFinished") which was only fired once. What I'm hearing is that there is none! Thanks for your help. FRunOnce it is.
+2  A: 

The normal order of execution for a Form is :

  • AfterConstruction: when the form and it components are fully created with all their properties.
  • OnShow: whenever the Form is ready to show (and, yes, any change causing a CM_SHOWINGCHANGED can trigger an OnShow)
  • Activate: whenever the Form takes the Focus

So, depending on what you need in ProcedureX, AfterConstruction might be enough, and is executed only once; just override it and add ProcedureX after inherited. It'll be after OnCreate.

If it is not the case, you can post a custom message to your Form from AfterConstruction, it will be queued and will reach your custom handler after the other messages have been handled.

In both cases, you would not need a extra boolean Field.

François
What about using OnCreate, then?
It might be OK. But AfterConstruction allows to always execute ProcedureX after whatever might be in OnCreate...
François
+5  A: 

If your code only needs to run once per form creation (or per application and the form is only created once per application run), put the code in the form's OnCreate handler. It is the natural place for it to go.

Nowadays (since D3 I think) the OnCreate fires at the end of the construction process in the AfterConstruction method. Only if you were to set OldCreateOrder to True (and it is False by default), might you get in trouble as that makes the OnCreate fire at the end of the Create constructor.

Marjan Venema
+2  A: 

@Sertac,

There's really no need for the FRUNOnce field; simply do OnShow=NIL as the first line of your FormShow method.

FYI, The "run once" idiom -- setting the event handler field to NIL in the first line of the event handler -- is also terribly useful for getting some code up-and-running once a form has been completely initialized. Put your code in a FormActivate method and, as the first line of the method, set OnActivate=NIL.

Erik Knowles
@Erik, that would only be possible if you've got nothing else to do in the event handler. If however you've got code in the handler that you want it to run whenever you f.i. unhide your form, you cannot nil the handler.
Sertac Akyuz
A: 

@Sertec,

Your code won't work either if you want it to run for every unhide event (you haven't put in any code to reset the frunonce field).

So your method would need to reset frunonce field, and mine would need to set OnShow=FormShow. Same difference, except that you need an additional field.

Erik Knowles
@Erik- If I'd be resetting the flag why would I have it? Example: I have to run procedure `ShowJustOnce` after the form becomes visible for the first time, on `OnShow`. And I've to run `UpdateInfo` every time a user causes to re-show the form, on `OnShow`. I cannot nil the handler because 'UpdateInfo' won't run. I have to use the flag because otherwise 'ShowJustOnce' would be running everytime the form is re-shown.
Sertac Akyuz
If you need some code in FormSHow that runs once and some that runs multiple times, then yes, you need a flag. That's not really relevant to the question that both of us were answering, which was merely "how do I have something only execute a single time when my form is initially shown".So, again...put it in the FormActivate method, and, as the first line of the method, put "OnActivate := NIL ;".If you want stuff to run every time the form is shown, and only once per actual visibility change, that's a different question.
Erik Knowles
@Erik - It is relevant and it is not a different question. I wouldn't suggest anyone to nil his event handler since I wouldn't know if there's code already there or not, and will not be in the future too... Like in on 'OnShow', one might have code in 'OnActivate' as well. I for instance, **have** code that re-shows forms which were previously hidden on 'OnDeactivate'. If I were to nil 'OnActivate', where would you suggest me to show them again?
Sertac Akyuz