Two forms in a program lazarus / 13-General ---------------------------------------------------------------------- KMagic · 2014-02-23 13:37 I have 2 forms. Form1 is the menu form. When clicking a button in form1, I want form2 to appear and form1 to disappear. When closing form2, I want form2 to disappear and form1 to appear again. How can I do that? By the way, what is the difference between form2.showmodal and form2.show? ---------------------------------------------------------------------- taazz · 2014-02-23 13:43 Why do you want to have form1 disappear? Showmodal will lock the entire application except the form that was shown as modal, in other words if you call form2.showmodal and form1 is visible then form1 will be unresponsive until form2 is closed. Showmodal is a function that it returns the user choice when closing form2 take a look on hte constants of mrclose, mrcancel, mrok etc for a list of predefined results. ---------------------------------------------------------------------- howardpc · 2014-02-23 13:53 Here's one way to do what you want. ---------------------------------------------------------------------- KMagic · 2014-02-23 17:24 Quote from: howardpc on February 23, 2014, 02:53:43 pmHere's one way to do what you want. Thank you. It works very well. @taazz: I got it. QuoteWhy do you want to have form1 disappear?   I'm just curious. Actually, making form1 unresponsive is OK for me. -------------------------------------   I have read a topic about multiple forms :http://forum.lazarus.freepascal.org/index.php?topic=3037.0    There is a procedure  like this: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); begin   Form2 := TForm2.Create(nil);   try     Form2.ShowModal;   finally     Form2.Free;   end; end; I have tried it and it works very well. However, I just don't understand one thing: Why do we put form2.free there? Will this line be executed after form2.showmodal? If so, why doesn't the  form2 disappear right after it appears? ---------------------------------------------------------------------- taazz · 2014-02-23 17:44 Quote from: KMagic on February 23, 2014, 06:24:04 pm   I have read a topic about multiple forms :http://forum.lazarus.freepascal.org/index.php?topic=3037.0    There is a procedure  like this: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); begin   Form2 := TForm2.Create(nil);   try     Form2.ShowModal;   finally     Form2.Free;   end; end; I have tried it and it works very well. However, I just don't understand one thing: Why do we put form2.free there? Will this line be executed after form2.showmodal? If so, why doesn't the  form2 disappear right after it appears? It is the way that a modal form works. Calling ShowModal will start a new loop that processes the OS events which does not return until you close the modal form and the old loop resumes, as a result form2.free will not be reached until you close the modal form in which case the modal form has already complete its purpose and it would be a waste of memory to keep it in memory. ---------------------------------------------------------------------- KMagic · 2014-02-24 11:27 Thank you, I understood it. However, are the two forms related to each other? I mean, for instance, I have form1 and form2 as I said. There is an edit box in form2 and a label in form1. When I change the text in edit box in form2 and click a button in form1, how can the label's caption change to the text in the edit box? ---------------------------------------------------------------------- howardpc · 2014-02-24 12:33 You need to ensure that form2 is visible to form1 (by including it in form1's uses clause) and then reference form2's data directly, something like (untested code) Code: [Select]procedure form1.Button1Click(Sender: TObject); begin   if Assigned(form2) then    Label1.Caption:=form2.Edit1.Text; end; ---------------------------------------------------------------------- KMagic · 2014-02-24 14:28 Thank you very much, howardpc. ---------------------------------------------------------------------- holdenjr · 2014-06-03 12:53 I have been trying many script create 2 form (form1 and form2 all with default name), but the problem still exist I follow this code: Code: [Select]procedure TForm1.Button1Click(Sender: TObject); begin   Form2 := TForm2.Create(nil);   try     Form2.ShowModal;   finally     Form2.Free;   end; end; But everytime I try to RUN the form1, it keep said: unit1.pas(25,16) Error: Identifier not found "TForm2" Am I miss something? Because some member said that above code is run without problem, but I always get the same problem. Thank you in advance ---------------------------------------------------------------------- taazz · 2014-06-03 12:54 uses unit2? maybe? ---------------------------------------------------------------------- holdenjr · 2014-06-03 13:34 Quote from: taazz on June 03, 2014, 02:54:44 pmuses unit2? maybe? Still same problem, when I change to this: Code: [Select]Form2 := unit2.Create(nil); The error now appear like this: unit1.pas(35,17) Error: Identifier not found "unit2" Any solution really appreciate... thank you in advance ---------------------------------------------------------------------- howardpc · 2014-06-03 13:47 You don't want to create a unit, but a form. The units should be something like this in outline (omitting most parts): Code: [Select]unit unit2; interface uses ...; type   Tform2=class(Tform) ...   end; ... var form2: TForm2; implementation .... end; and then Code: [Select]unit unit1; interface uses ... ; type TForm1=class(TForm) ... end; var   form1: TForm1; implementation uses unit2; ... procedure TForm1.Button1Click(Sender: TObject); begin   Form2 := TForm2.Create(nil);   try     Form2.ShowModal;   finally     Form2.Free;   end; end; ... end;