Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 27th, 2012, 05:55 AM
newsoul newsoul is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 newsoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 49 m 37 sec
Reputation Power: 0
Switching between forms

I'm having some trouble switching between forms when I use .destroy. If I only use .hide or .close, I end up increasing the memory usage of my app each time I switch.

I have a button in form 2 which, when clicked, destroys form 2 and shows form 1. How can I access form 2 again (from form 1) if I've destroyed it? I keep getting "Abstract Error" after the second time I click the second button.
Here's the code:

procedure TForm1.Button1Click(Sender: TObject); //in unit1
var j:byte;
begin
assignfile(f,'dif.txt');
rewrite(f);
write(f,'1');
closefile(f);
assignfile(g,'backclicked.txt');
reset(g);
read(g,i); // f,g and i are public in unit1
closefile(g);
if i<>1 then
begin
form1.hide;
form2.show;
end else
begin
form2:=TForm2.Create(self);
form2.show;
form1.hide;
end;
end;

procedure TForm2.BackClick(Sender: TObject); //in unit2
var f:textfile;
begin
assignfile(f,'backclicked.txt');
rewrite(f);
write(f,'1');
closefile(f);
form1.show;
form2.destroy;
end;

Reply With Quote
  #2  
Old October 27th, 2012, 09:40 AM
emailx45 emailx45 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 44 emailx45 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 7 m 32 sec
Reputation Power: 5
Thumbs up

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;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > Switching between forms

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap