[Lazarus] Where is form instance being NIL'ed

Michael Van Canneyt michael at freepascal.org
Tue Oct 14 16:33:44 CEST 2008



On Tue, 14 Oct 2008, Graeme Geldenhuys wrote:

> On Tue, Oct 14, 2008 at 3:50 PM, Michael Van Canneyt
> <michael at freepascal.org> wrote:
> >> When a new form is created using Application.CreateForm(), the form's
> >> global instance variable is passed in as well. Now if that form's
> >> CloseAction = caFree, where is that instance variable assigned nil
> >> after it was freed?
> >
> > It is not, unless you set it to nil in the BeforeDestroy event.
> >
> > Using that variable is a very bad idea anyway.
> 
> I hate those global form variables as well, but how else do you handle
> non-modal forms? For example. I create a non-modal TaxCodeListing
> form. The user used the main form's menu to get to TaxCodeList.  If
> the form already exists, it should bring it to the front and not
> create another instance. To do that, if check if the global form
> variable = nil or not. After the first create and even if the user
> actually closed the form (not just sitting in the background), that
> global form variable in now always <> nil.
> 
> So when the user selects the menu option to view the TaxCodeList form
> a little later. The apps sees that the global form variable <> nil and
> tries to call .ActivateWindow() and I get a lovely exception error.
> :-(
> 
> function DisplayTaxCodeList: Boolean;
> begin
>   Result := False;
>   if not Assigned(TaxCodeListForm) then
>     fpgApplication.CreateForm(TTaxCodeListForm, TaxCodeListForm)
>   else
>     TaxCodeListForm.ActivateWindow;
>   TaxCodeListForm.Show;
>   Result := True;
> end;
> 
> 
> Any better suggestions for managing non-modal forms?

Sure. All forms are owned by the global application, so:

 C:=fpgApplication.FindComponent('TaxCodeListForm');
 If (C<>Nil) then
   TaxCodeListForm(C).ActivateWindow
 else
   fpgApplication.CreateForm(TTaxCodeListForm, TaxCodeListForm)

Michael.



More information about the Lazarus mailing list