
October 27th, 2012, 09:40 AM
|
|
Contributing User
|
|
Join Date: Sep 2008
Posts: 44
Time spent in forums: 10 h 7 m 32 sec
Reputation Power: 5
|
|
PLease: look the help for more explanation
Look: you ALWAYS CREATE form2 if not exists! one ERROR coding, normaly, you create only one time (in a place out the procedure that you avaliable the your existence.
try use one function, as:
function havetheformx(xFormClass:TCustomForm):boolean;
var
i:integer;
begin
for i:0 to Screen.Forms.count-1 do
if screen.forms[i].class IS "or = xFormClass then
begin //already exist
result:=true;
exit;
end;
end;
if havetheformx( TForm2 ) then
if not Form2.showing then
form2.showmodal;
------------------
application form main or for example one unit (with no forms)
unit
uses
uform2, etc.......
interface
....
var
xform2:tform2;
implementation
...
initialization
xform2:=tform2.create(application or nil or self)
finalization
xform2.free;
end.
-------------------
when you ute NIL = the form is responsable for your destory
SELF = the Owner destroy the component (form2)
application = the application destroy when terminate
-------------------
in form2 on event Onclosequery
Action = cafree = destroy the form (see help for another options)
------------------
if you needed to use very time the FORM2, so, dont destroy it (free), just HIDE
my sugestion:
if you use on DataModule, make the form2 in!
procedure datamodule.oncreate;
begin
form2:= tform2.create(self); //datamodule destroy
end; // self = datamodule component here
if = nil, so
procedure datamodule.ondestroy;
begin
if Assigned( form2 ) then
form2.free;
end;
--------------
in code normal, use FORM2.hide for hide it!
for use a bit memory try make the form2 all the time that need!
procedure tformX.buttonclickevent
var
xform2:tform2;
begin
try
try
xform2:=nil; // for assigned function better action
xform2:= tform2.create(nil); //you destroy it
x.form2.showmodal;
except
on e:exception do showmessage('error: '+e.message);
end;
finally
if assigned(xform2) then
FreeAndNil( xform2 ) ; //see help for freeandnil
end;
|