#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep 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;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2008
    Posts
    55
    Rep Power
    6

    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;

IMN logo majestic logo threadwatch logo seochat tools logo