multi- forms lazarus / 13-General ---------------------------------------------------------------------- johnf · 2006-03-11 01:10 Hi, In the VB world I can open the same form several times and get new instances of the form using the code below: PUBLIC SUB button_click() DIM custform AS CustMaintenance custform = NEW CustMaintenance custform.Show Everytime I click on the button I will get a new form.  Three clicks will get three forms. But the pascale code below only opens one form - even if I click 10 times. procedure TForm1.Button1Click(Sender: TObject); begin   form2:=Tform2.Create(nil); //form2:=Tform2.CreateNew(   //try    form2.Show;   //finally    //form2.Free;  // end;   end; How can I do what I do with VB?????? John ---------------------------------------------------------------------- Anonymous · 2006-03-11 07:09 I am just an occasional Delphi programmer, but I bet you are doing a mistake there. See this: procedure TForm1.Button1Click(Sender: TObject); begin form2:=Tform2.Create(nil); //recreate form 2 and replace it //form2:=Tform2.CreateNew( //try form2.Show; //finally //form2.Free; // end; So if I understood right, you are replacing the form everytime you click on that button. Plus the pointer to the form2 is lost if you recreate the form without freeing it first. This means a memory leak, unless you free the form on one of its own events, like if you coded Form2.Close: begin   Self.Free; end; I think you would need to either create an array or list where you store the created forms if you want to free them manually at Form1, because to free them, you will need the pointer. Maybe the answer is something like: procedure TForm1.Button1Click(Sender: TObject); Var DumbForm: TForm; begin   DumbForm:= TForm2.Create(nil);   DumbForm.Show; end; You should free it on the FormClose event of TForm2 then. ---------------------------------------------------------------------- Anonymous · 2006-03-11 07:47 I'm a newbie too!  But I think I understand.  I'll test it and get back. John ---------------------------------------------------------------------- Anonymous · 2006-03-11 07:56 Thanks for the help - worked as expected....... ---------------------------------------------------------------------- Marc · 2006-03-13 10:48 Quote from: "Anonymous"So if I understood right, you are replacing the form everytime you click on that button. Plus the pointer to the form2 is lost if you recreate the form without freeing it first. This means a memory leak, unless you free the form on one of its own events, like if you coded Form2.Close: begin   Self.Free; end; Just a note: It is better to not call free in an event. This may result in an exception, since after the event the object may be refecenced internally. If you need to free a form in an event, use Release. For this specific case it is even better to use the OnClose event and set the CloseAction to caFree. Then the form is freed by the LCL itself. ---------------------------------------------------------------------- matthijs · 2006-03-13 11:13 If a form is shown modally you can use the following construction safely: Code: [Select]   aForm := TaForm.Create(nil);   try     aForm.ShowModal;   finally     aForm.Free;   end;   // continue with code Remember to keep the actual creation out of the try..finally, because if something goes wrong during creation there will be nothing to free. ---------------------------------------------------------------------- johnf · 2006-03-15 05:51 caFree could you provide a simple example? John ---------------------------------------------------------------------- matthijs · 2006-03-15 07:18 If you make an OnClose event is looks like:Code: [Select]procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin end; All you have to do is adding a statement like: Code: [Select] CloseAction := caFree;