Recent

Author Topic: how to get rid of it (hide) a CreatedForm at runtime and show the main Form a,?  (Read 4191 times)

userx-bw

  • Full Member
  • ***
  • Posts: 178
Creating a Form at runtime I followed the instructions from this
web page http://freepascalanswers.wordpress.com/2012/01/22/create-forms-at-run-time/

it works, but now I have to close that Form and show my main Form again?
I tried
Code: [Select]
procedure TForm2.MenuItem3Click ( Sender : TObject ) ;
begin

  Form2.Close;

  Form1.Visible:=true;
  Form1.Show;
  Form1.randomColorsCB.Checked:=false;
end; 
Code: [Select]
procedure TForm2.FormClose ( Sender : TObject; var CloseAction : TCloseAction
  ) ;
begin
  CloseAction:= caFree;
end;
gives error CustomForm.inc Line 2070
Code: [Select]
  IsMainForm: Boolean;
begin
  if fsModal in FFormState then
    ModalResult := mrCancel
  else
  begin
    if CloseQuery then
    begin
.....   
   
   
trying this option I get a runtime error
   
Code: [Select]
Form2.Visible:=false; 
     
     
trying this option
     
Code: [Select]
Form2.Hide; 
     

     
I get this error in CustomForm.inc Line 2180
 
Code: [Select]
  SIGSEGV'.

procedure TCustomForm.Hide;
begin
  Visible := False;
end;

so the question is how do I get the second From to go away and the main form to show again?


« Last Edit: February 10, 2014, 02:05:15 am by userx-bw »
My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

howardpc

  • Hero Member
  • *****
  • Posts: 4144
There are always several ways to kill a cat. The following way works well for me, but does not free the second form (only to recreate it when needed later).
In your project have a Close TBitBtn with its Kind set to bkClose. This will simply close the form (set its ModalResult to mrClose) after use, but not free it. Your basic project skeleton would then be a main form:

Code: [Select]
unit mainForm;

{$mode objfpc}{$H+}

interface

uses
  Forms, StdCtrls, secondForm, Classes;

type

  { TForm1 }

  TForm1 = class(TForm)
    BShowForm2: TButton;
    procedure BShowForm2Click(Sender: TObject);
  end;

var
  Form1: TForm1;
  Form2: TForm2=nil;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.BShowForm2Click(Sender: TObject);
begin
  if Form2=nil then
    Form2:=TForm2.Create(Application);
  form2.Show;
end;

end.   

and your second form skeleton would be:

Code: [Select]
unit secondForm;

{$mode objfpc}{$H+}

interface

uses
  Forms, Buttons;

type

  { TForm2 }

  TForm2 = class(TForm)
    BClose: TBitBtn;
  end;

implementation

{$R *.lfm}

end.     

Make sure you set the second form not to be autocreated (via Project Options or by editing your project's .lpr).

userx-bw

  • Full Member
  • ***
  • Posts: 178
This will simply close the form (set its ModalResult to mrClose) after use, but not free it.

if it does not free the form then if on the main form that calls it then, every time I call for it to be, then it is created thus it is creating a new Form2 every time, thus now I am eating up memory by having a bunch of created form2's ? Unless I have in the onClose Event in Form2.
Code: [Select]
procedure TForm2.FormClose ( Sender : TObject; var CloseAction : TCloseAction ) ;
begin
  Form1.Visible:=true;
  Form1.Show;
  Form1.randomColorsCB.Checked:=false;

  CloseAction:= caFree;
end;   
My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

howardpc

  • Hero Member
  • *****
  • Posts: 4144
No, the form is not recreated each time, merely removed from view.
The instantiated class is still there in memory (ready to be reshown without needing to be recreated), it is just that your GUI is not at that moment displaying it.
The single instance of Form2 is freed by Application (its owner) when the application closes (unless Form2 is never shown by the app, in which case it never gets created and so never needs freeing).
« Last Edit: February 10, 2014, 03:19:57 pm by howardpc »

 

TinyPortal © 2005-2018