Board index » delphi » Unable to close form on error

Unable to close form on error

I have the following code:

...

procedure TfrmLager.FormCreate(Sender: TObject);
var
   IniFil: TIniFile;
begin
     IniFil:=TIniFile.Create('lager.INI');
     tLager.DataBaseName:=IniFil.ReadString('files', 'dbpath',
'ERROR');
     try tLager.Active:=True;
     except on EDatabaseError do
     begin
          MessageDlg('Finner ikke databasen!' +
               ' DBPath=' + tLager.DatabaseName, mtWarning, [mbOK],
0);
          frmLager.Free;
     end;
     end;
     {Frigir minne fra inifilen}
     IniFil.Free;
     {setter opp ?vrige controller}
     qSalg.DataBaseName:=tLager.DataBaseName;
     qSalg.Active:=True;
     rbDato.Checked:=True;
end;

...

My problem is that if the EDataBaseError is true, the frmLager.Free
does *not* stop executing the program.
The MessageDLG goes ok, but it seems to skip the Free and continues
the program.
I've tried Close and Free but none works.

Pedro

 

Re:Unable to close form on error


Pedro E. Thormodsen (pedro.thormod...@login.eunet.no) wrote:
: I have the following code:

: ....

: procedure TfrmLager.FormCreate(Sender: TObject);
: var
:    IniFil: TIniFile;
: begin
:      IniFil:=TIniFile.Create('lager.INI');
:      tLager.DataBaseName:=IniFil.ReadString('files', 'dbpath',
: 'ERROR');
:      try tLager.Active:=True;
:      except on EDatabaseError do
:      begin
:           MessageDlg('Finner ikke databasen!' +
:                ' DBPath=' + tLager.DatabaseName, mtWarning, [mbOK],
: 0);
:           frmLager.Free;
:      end;
:      end;
:      {Frigir minne fra inifilen}
:      IniFil.Free;
:      {setter opp ?vrige controller}
:      qSalg.DataBaseName:=tLager.DataBaseName;
:      qSalg.Active:=True;
:      rbDato.Checked:=True;
: end;

: ....

: My problem is that if the EDataBaseError is true, the frmLager.Free
: does *not* stop executing the program.
: The MessageDLG goes ok, but it seems to skip the Free and continues
: the program.
: I've tried Close and Free but none works.

: Pedro

I dont think you can prevent the form from being created by a call to
.Free in the form's OnCreate handler. What I usually do is define a
boolean flag named e.g. bEverythingOK in the form's private section,
which I initialize FALSE in the first line of OnCreate and set to TRUE
if the creation has been completed successfully.  
Then I employ the OnActivate handle by inserting the following:

Procedure TFrmLager.OnActivate(Sender: TObject);
begin
  if not everything OK then Close;
end;

This works satisfactory for me. However, if someone has a different (and
probably more elegant solution, I'd be glad to know.

Hope this helps

Andy

Re:Unable to close form on error


Quote
Pedro E. Thormodsen wrote:
> =
> I have the following code:
> =
> ...
> =
> procedure TfrmLager.FormCreate(Sender: TObject);
> var
>    IniFil: TIniFile;
> begin
>      IniFil:=3DTIniFile.Create('lager.INI');
>      tLager.DataBaseName:=3DIniFil.ReadString('files', 'dbpath',
> 'ERROR');
>      try tLager.Active:=3DTrue;
>      except on EDatabaseError do
>      begin
>           MessageDlg('Finner ikke databasen!' +
>                ' DBPath=3D' + tLager.DatabaseName, mtWarning, [mbOK],
> 0);
>           frmLager.Free;
>      end;
>      end;
>      {Frigir minne fra inifilen}
>      IniFil.Free;
>      {setter opp =F8vrige controller}
>      qSalg.DataBaseName:=3DtLager.DataBaseName;
>      qSalg.Active:=3DTrue;
>      rbDato.Checked:=3DTrue;
> end;
> =
> ...
> =
> My problem is that if the EDataBaseError is true, the frmLager.Free
> does *not* stop executing the program.
> The MessageDLG goes ok, but it seems to skip the Free and continues
> the program.
> I've tried Close and Free but none works.
> =
> PedroYou should raise your own error in this case.

See: raise command in the help files
HJW

Re:Unable to close form on error


pedro.thormod...@login.eunet.no (Pedro E. Thormodsen) wrote:

Quote
>I have the following code:
>...
>procedure TfrmLager.FormCreate(Sender: TObject);
>var
>   IniFil: TIniFile;
>begin
>     IniFil:=TIniFile.Create('lager.INI');
>     tLager.DataBaseName:=IniFil.ReadString('files', 'dbpath',
>'ERROR');
>     try tLager.Active:=True;
>     except on EDatabaseError do
>     begin
>          MessageDlg('Finner ikke databasen!' +
>               ' DBPath=' + tLager.DatabaseName, mtWarning, [mbOK],
>0);
>          frmLager.Free;
>     end;
>     end;
>     {Frigir minne fra inifilen}
>     IniFil.Free;
>     {setter opp ?vrige controller}
>     qSalg.DataBaseName:=tLager.DataBaseName;
>     qSalg.Active:=True;
>     rbDato.Checked:=True;
>end;
>...
>My problem is that if the EDataBaseError is true, the frmLager.Free
>does *not* stop executing the program.
>The MessageDLG goes ok, but it seems to skip the Free and continues
>the program.
>I've tried Close and Free but none works.
>Pedro

OTTOMH, I would try rearranging things as follows,
and use frmLager.Release; instead of frmLager.Free.
You might want to handle inifile-related exceptions
too, which I haven't shown.

Hope this is a useful reaction. I haven't tested it.
Regards,
Bengt Richter
-----------------------------------------------------
var
   IniFil: TIniFile;
begin
     IniFil:=TIniFile.Create('lager.INI');
     tLager.DataBaseName:=
              IniFil.ReadString('files', 'dbpath','ERROR');

     {Frigir minne fra inifilen}
     IniFil.Free;
     try
        tLager.Active:=True;
       {setter opp ?vrige controller}
       qSalg.DataBaseName:=tLager.DataBaseName;
       qSalg.Active:=True;
       rbDato.Checked:=True;
     except
       on EDatabaseError do begin
          MessageDlg('Finner ikke databasen!' +' DBPath='
               + tLager.DatabaseName, mtWarning, [mbOK],0);
          frmLager.Release;
     end;
     end;
end;
----------------------------------------------------------

Other Threads