Robust Form
Initialization
Written by Poul Halgaard, Blibler Software 26. Feb. 2008. Copyright.
Search
keys:
form-creation initialization robust form-show start-up
When you
need to use a form in you Delphi program, it is important how you control the
creation and initialization, if you want the code to be completely failsafe. This
article describes how this can be accomplished with simple means and describes
the problems you can get into, if you do not consider this.
In Delphi a
form has a FormCreate and a FormShow event, these two
is where you normally initialize the form or othervice
you initialize it in the code wrapping the displaying of the form, e.g. like
this:
var
f:
TForm;
FormNotCancelled:
Boolean;
begin
f
:= TForm.Create(nil);
try
f.Initialize;
FormNotCancelled := f.ShowModal = mrOk;
if
FormNotCancelled then
ShowMessage(‘Hello’);
finally
f.Free;
end;
This is a
nice way to do it and errors are handled correctly.
If however circumstances is that you do not want an external call to a
Initialize method, but you want to use FormCreate or
FormShow instead, you need to be careful.
If code in
the FormCreate event fails, an exception is thrown,
but the expected result is not what then actually happens. This has to do with
the Windows message system.
The
remaining part of the FormCreate is skipped, but then
the FormShow is invoked!
So if you
want to use FormCreate for some initialization, you
actually need to check that it completed in the FormShow.
E.g. use a
flag like FormCreatedCompletedSuccessfully.
You
actually need to check it in all code until the form is successfully shown on
the screen. This is errorprone, so the approach with
an initialize method is recommended.
Sometimes
you want the form to display and then make some timeconsuming
initialization.
You can use
the Initialize method for this. If you want 2 steps you would ofcause need both an Initialize and RemainingInitialize
methods for example. The RemainingInitialize you could
call in the FormShow event.
If
something goes wrong there, there is a way to handle it. Close the form and
raise an exception.
When a
control has been created there is a good chance it can start generating event.
But that
might lead to other errors, if the form failed during its initialization!
It is not
safe to begin to execute code in the events before the form has been completely
initialized and displayed. Except if you really know what is going on.
To prevent unwanted
events this you can use a flag like:
FormShowCompletedSuccessfully
You set
this to True right before you exit FormShow and in all dangereous
events you start with this code:
If not FormShowCompletedSuccessfully then
Exit;
For this
and other reasons you should be very alerted when
using events. As the application grows you will get more and more unexpected
situations if you use a lot of events.
Some
controls can fire after the form has been closed.
You can
then set FormShowCompletedSuccessfully to False in
the beginning of FormClose to prevent this.