Problem Closing Forms

Question:

I created the main search screen form SearchScrn (fsNormal) with the ability to create as many new forms on the fly (they are not children of MDI form) as possible after a search is refined. I can close all of these forms using one button click event:

procedure TSearchScrn.CloseAllBtnClick(Sender: TObject);beginSearchScrn.Close;end;  { TSearchScrn.CloseAllBtnClick }

This works, but if I want to start new search, I try to close all forms and create a new main SearchScrn form:

procedure TSearchScrn.SearchListBtnClick(Sender: TObject);var  SearchStr1     : String[20];  SearchOp       : String[ 3];  SearchStr2     : String[20];begin  if Pos('New',SearchListBtn.Caption)>0 then     begin     SearchStr1:=SearchFld.AsString;     SearchOp  :=SearchOperator.Text;     SearchStr2:=SearchFld2.AsString;     SearchScrn.Close;     SearchScrn := TSearchScrn.Create( Self );     with SearchScrn do          begin          SearchFld.AsString:=SearchStr1;          SearchOperator.Text:=SearchOp;          SearchFld2.AsString:=SearchStr2;          MakeSearch;          ShowModal;          end;     end     else       MakeSearch;end;  { TSearchScrn.SearchBtnClick }

For some reason SearchScrn.Close does not close all the forms and instead leaves them on the screen.

Answer:
One thing that you should do is to ‘Free’ the form after closing it. Also, to make doubly sure that the form variable’s reference is gone, set it to ‘nil’ after freeing it.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: