DeCordova
asked on
Delphi 3 Close vs Free to close a form
I have been told that when closing a form, Close merely hides the form whereas Free not only closes the form but frees up the resources. My problem is this:
I am writing a program to administer a questionnaire which will consist of a large number of forms with each leading to the next or returning to the previous form, or being able to cancel the operation from any form. Example of the code I am using (from Form2) is:
procedure CancelButtonClick(Sender: Tobject);
begin
Close;
end;
procedure NextButtonClick(Sender: Tobject);
begin
DoSomething;
Form2.Hide;
Form3 := TForm3.Create(Self);
Form3.ShowModal;
Close;
end;
procedure BackButtonClick(Sender: Tobject);
begin
DoSomething;
Form2.Hide;
Form1 := TForm3.Create(Self);
Form1.ShowModal;
Close;
end;
This works great and I am having no problem, but if the close does not free the resources used by the form I will soon run out of them when moving forward and backward through the forms. I have tried using Free instead of close, but this always gives me an access error when canceling from a form.
I am writing a program to administer a questionnaire which will consist of a large number of forms with each leading to the next or returning to the previous form, or being able to cancel the operation from any form. Example of the code I am using (from Form2) is:
procedure CancelButtonClick(Sender: Tobject);
begin
Close;
end;
procedure NextButtonClick(Sender: Tobject);
begin
DoSomething;
Form2.Hide;
Form3 := TForm3.Create(Self);
Form3.ShowModal;
Close;
end;
procedure BackButtonClick(Sender: Tobject);
begin
DoSomething;
Form2.Hide;
Form1 := TForm3.Create(Self);
Form1.ShowModal;
Close;
end;
This works great and I am having no problem, but if the close does not free the resources used by the form I will soon run out of them when moving forward and backward through the forms. I have tried using Free instead of close, but this always gives me an access error when canceling from a form.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In fact it denpends on the way you quit a form.
If you close the form, then use the whoe method to make it reappear
If you destroy the form (with the free method) then you need to create it again to be able to show it again.
As far as i know, the close method does not free the ressources. you have to call the free method to do it or it will be done when you quit the application.
The errors you get with the way you use the free method occour because you call the free method from the object itself. i suggest you to use a structure like the following one
var
actual_form:byte;
sub main;
begin
actual_form:=1;
while (actual_form>0) and (actual_form<max_form) do
case actual_form of
1:
begin
form1:=Tform1.create(self) ;
form1.showmodal;
form1.free;
end;
2:
.....
else
actual_form:=0;
end;
end;
procedure form1.backbutton;
begin
dec(actual_form);
end;
procedure form1.nextbutton;
begin
inc(actual_form);
end;
procedure form1.cancelbutton;
begin
actual_form:=0;
end;
If you close the form, then use the whoe method to make it reappear
If you destroy the form (with the free method) then you need to create it again to be able to show it again.
As far as i know, the close method does not free the ressources. you have to call the free method to do it or it will be done when you quit the application.
The errors you get with the way you use the free method occour because you call the free method from the object itself. i suggest you to use a structure like the following one
var
actual_form:byte;
sub main;
begin
actual_form:=1;
while (actual_form>0) and (actual_form<max_form) do
case actual_form of
1:
begin
form1:=Tform1.create(self)
form1.showmodal;
form1.free;
end;
2:
.....
else
actual_form:=0;
end;
end;
procedure form1.backbutton;
begin
dec(actual_form);
end;
procedure form1.nextbutton;
begin
inc(actual_form);
end;
procedure form1.cancelbutton;
begin
actual_form:=0;
end;
Have you ever thought of using the TNoteBook component??
You can use it to display different questions but still using the same form...
You can use it to display different questions but still using the same form...
ASKER
That did it! I feel stupid because I have used that while do loop for the same purpose for years and didn't think of using it here. Thanks for your prompt and accurate answer.
ASKER
By the way, I think your answer is a record, I only posted the question a few minutes ago.