Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form close problem

Status
Not open for further replies.

scoopbh

Programmer
Feb 28, 2003
6
BR
Hi,

I'm building a software with many forms. I'm having problem to browse through one form to another.

When I close a form their variables, items in lists, objects are not free, when I open the form again the value are still there.

I'm using formX.show to open a form, and I tried formX.free on the onclose event to free the components, also tried Action:=cafree, but it was unsuccessful. When I reopen the closed form I get a exception error.

What is the best way to close forms and free their variabels?
 
What you are asking is not easy to answer. Fundamentally, the answer is "it depends."

Is the form modal or modeless?
Is the form automatically created by Delphi?

Generally, if Delphi creates an object, you should let Delphi free it. If you create an object, you should free it.

I usually have Delphi create just the main form. Then I create/destroy other forms as needed. (Sometimes it is useful to keep a form from start to finish and just show/hide as required. This keeps the values and allows them to be accessed from other forms.)

For most modal dialog boxes, I create a global function (in the unit that contains the form) which does all of the create/free stuff and use formal parameters to pass data into and out of the dialog. For example, a simple data input dialog might look like this:
Code:
unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

function GetNameAndNumber( APrompt:string;
                  var AName:string; var ANumber:integer ):boolean;

type
  TdlgGetNameAndNumber = class(TForm)
    stPrompt: TStaticText;
    dfName: TEdit;
    dfNumber: TEdit;
    pbOk: TButton;
    pbCancel: TButton;
    procedure pbOkClick(Sender: TObject);
    procedure pbCancelClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  dlgGetNameAndNumber: TdlgGetNameAndNumber;

implementation

{$R *.DFM}

function GetNameAndNumber( APrompt:string;
                  var AName:string; var ANumber:integer ):boolean;
begin
  Result := False;
  dlgGetNameAndNumber := TdlgGetNameAndNumber.Create( nil );
  try
    with dlgGetNameAndNumber do
      begin
        stPrompt.Caption := APrompt;
        ShowModal;
        if ModalResult = mrOk then
          begin
            AName := dfName.Text;
            ANumber := StrToInt(dfNumber.Text);
            Result := True;
          end;
      end;
  finally
    dlgGetNameAndNumber.Free;
  end;
end;

procedure TdlgGetNameAndNumber.pbOkClick(Sender: TObject);
begin
  if (dfName.Text <> '') and (dfNumber.Text <> '') then
    ModalResult := mrOk
  else
    ShowMessage ('Name and Number required.');
end;

procedure TdlgGetNameAndNumber.pbCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

end.
And it can be invoked simply by using code like this:
Code:
uses Unit5;

procedure TForm1.Button3Click(Sender: TObject);
var
  sPrompt,sName:string;
  nNumber:integer;
begin
  sPrompt := 'Enter office data:';
  if GetNameAndNumber( sPrompt, sName, nNumber ) then
    ShowMessage( 'New name: ' + sName + '  New number: ' + IntToStr(nNumber) )
  else
    ShowMessage( 'Cancel' );
end;
 
The only thing I'd like to add is not to use the global variable. Remove it.

When creating a new form always declare the form variable localy like this:


function GetNameAndNumber( APrompt:string; var Name:string; var ANumber:integer ):boolean;

var
dlGetNameAndNumber: TdlgGetNameAndNumber;
begin
Result := False;
dlgGetNameAndNumber := TdlgGetNameAndNumber.Create( nil );
try
with dlgGetNameAndNumber do
...
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top