Solved

Delphi 3 Close vs Free to close a form

Posted on 1998-03-13
5
166 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:DeCordova
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
pjdb earned 100 total points
ID: 1360483
If you create the form each time you call it, for sure you will run out of ressouces. you have to create the form only once in the all program.
on the cancel button, you don't need to free the form, the program will do it at the termination.

JDB
0
 

Author Comment

by:DeCordova
ID: 1360484
If I create the form only once what happens when I close it and then need to return to it by going back in the questionnaire? When I use the Close command, am I freeing up the resources or do I need to use Free to do it? I tried using Free instead of Close but get errors when I try to cancel one of the forms. My main concern is running out of resources because I will be using a very large number of forms.

By the way, I think your answer is a record, I only posted the question a few minutes ago.


0
 
LVL 1

Expert Comment

by:pjdb
ID: 1360485
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;

0
 

Expert Comment

by:ermac
ID: 1360486
Have you ever thought of using the TNoteBook component??
You can use it to display different questions but still using the same form...

0
 

Author Comment

by:DeCordova
ID: 1360487
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.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question