Using multiple forms lazarus / 13-General ---------------------------------------------------------------------- dani.user · 2006-10-28 09:01 Hello I am new to lazarus. I want to ask you how I could do something like this: I have created 2 forms and I want to show the second one when I click on a button on the first form. Thank you ---------------------------------------------------------------------- theo · 2006-10-28 09:42 File -> New Form then in Unit1 Code: [Select]... implementation uses Unit2; { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin   Form2.ShowModal; end; ... ---------------------------------------------------------------------- matthijs · 2006-10-29 10:52 I usually remove all forms (except main form) from autocreate list. So therefor I would prefer to do it like this: Code: [Select] procedure TForm1.Button1Click(Sender: TObject); begin   Form2 := TForm2.Create(nil);   try     Form2.ShowModal;   finally     Form2.Free;   end; end; My opinion is, that this is safer, as you can never be sure when the user clicks on Button1 Form2 is assigned. ---------------------------------------------------------------------- theo · 2006-10-29 12:02 Quote from: "matthijs"I usually remove all forms (except main form) from autocreate list. --- My opinion is, that this is safer, as you can never be sure when the user clicks on Button1 Form2 is assigned. I don't understand your comment and why you want to make it more complicated than necessary for dani.user. Why shouldn't Form2 be assigned? ---------------------------------------------------------------------- CCRDude · 2006-10-29 13:04 When you've reached a dozen forms or so, it makes sense from a memory consumption point to create modal forms dynamically. Anyway, I only see Form2.ShowModal; mentioned (which means a modal dialog - the user can't get back to the main form until he closes Form2) - but whether dani.user wanted a modal or nonmodal dialog (just Form2.Show) is still unclear, so this discussion may be unnecessary ---------------------------------------------------------------------- matthijs · 2006-10-29 17:09 Quote from: "theo"I don't understand your comment and why you want to make it more complicated than necessary for dani.user. Why shouldn't Form2 be assigned? I donot want to make things more complicated, but when somebody starts I like him to start off correctly. It may seem overdone, but picture this: An application with a lot fo forms. Each form created in autocreate makes your program take longer to start. This is not such a big thing. But something more important is this: You are programming an event driven application not a procedural application. In the latter case (we use in the old days) you knew where a user came from and where he was heading to. Nowadays you tell what is happening if a user performs a certain action, but you do not know when a user will perform this action. So you do not know what is assigned and what is not. Some where else in your code form2 might be released. Or it doesnot even have to be your own code. It might be in a part of the application your colleague worked on. So just start of the right way and take this kind of stuff into account. It makes for sound programming. The try .. finally is something you should always use when you create an instance of some type. This way you can make sure what you create is released even if something goes wrong. ---------------------------------------------------------------------- theo · 2006-10-29 18:52 QuoteI donot want to make things more complicated, but you are! ;-) Sorry matthijs but your comment is inadequate for a newbie and what you're saying is not very convincing anyway. Why should Lazarus (and Delphi) autocreate the Form by default, if it's that wrong? This is at best some sort of fine tuning for a big application but completly usesless for newbies. Quote Some where else in your code form2 might be released. Or it doesnot even have to be your own code. It might be in a part of the application your colleague worked on. Are you paranoid ? ;-) How can you be sure about any part of your code then? Some bad guy might as well have written "Application.free" in a hidden place of the code! ;-) ---------------------------------------------------------------------- matthijs · 2006-10-29 21:20 Quote from: "theo" Sorry matthijs but your comment is inadequate for a newbie and what you're saying is not very convincing anyway. Why should Lazarus (and Delphi) autocreate the Form by default, if it's that wrong? This is at best some sort of fine tuning for a big application but completly usesless for newbies. Why do they give the option to "not auto create forms". (But this would restart an old discussion on why anybody would declare global form variables in an unit.) And what is paranoid about not knowing in what order a user performs his actions? And why should fine tuning be left until it is too late? And why should we not tell newbies how things should be done? I have seen this happen with a lot of newbies (and I have given quite a few courses for newbies ) So I always tell them to remove the auto creation and create a form when they need it. You do not want to know how many bugs this has solved. You can not start early enough getting used to using try .. except or try .. finally. ---------------------------------------------------------------------- RudieD · 2006-11-02 18:42 I agree with Matthijs, if you do it, do it right. I also usualy check if if the instance is the global instance and then set Form2 = nil. Bad habbits die hard ! ---------------------------------------------------------------------- mrbruno7447 · 2014-07-03 01:10 Thanks for the help but there is only one problem, the other forms are still open and after opening some other forms, it can make the computer very slow. How can I close the other forms that i'm not using? ---------------------------------------------------------------------- fpcoder · 2014-07-03 04:05 If you keep the autocreate of the form, you can use Show, and Hide/Close. Close will not Free the form unless you add CloseAction:=caFree in the forms OnClose Event. So when you call Show again, the same form will show up. Like matthijs I've always created all forms manually, and removed them from Project Inspector->Autocreated forms. Differently from matthijs I use the OnClose event with caFree and just call Close to close and free the form. You don't have to (and mustn't) call Free manually in this case. Just call Close from a button for example. If you use the OnClose event to free the form you can use plain Show to show the form and thus get at the main form too while the second form is open. With matthijs method you must call ShowModal. Which might be just what you want anyway. ---------------------------------------------------------------------- DavidTh30 · 2017-09-02 02:02 I alway Do some thing like this. At the head of Form1: unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, Forms, Controls, ...,  unit2; . . . procedure TForm1.Button1Click(Sender: TObject); begin    Form2.Label1.Caption:='OK';   Form2.ShowModal;  //form2.Show; end; At the head of Form2: unit Unit2; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; . . . implementation uses   unit1; {$R *.lfm} { TForm2 } procedure TForm2.FormClose(Sender: TObject; var CloseAction: TCloseAction); begin   Form1.Label1.Caption:='Form2 close'; end; This way you can call and send the message between Form1 and Form2 each other. ---------------------------------------------------------------------- molly · 2017-09-02 02:18 Quote from: DavidTh30 on September 02, 2017, 04:02:18 amI alway Do some thing like this. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i interfaceuses  Classes, SysUtils, Forms, Controls, ...,  unit2; Which is a "circular unit reference" waiting to happen. The only reason you would really want to include another form/module unit in the uses clause of your interface is when you directly (at design-time) want to be able to assign events/methods that are located in that unit. In all other cases first try to do it in the uses clause in the implementation section. That works for most (if not all) cases and in case it doesn't then you can always opt for putting the unit in the uses clase of the interface section. As a general rule of thumb: when that is needed that most probably means you are using global definitions (and defining those inside the wrong unit, use a additional 3-th unit for that). ---------------------------------------------------------------------- DavidTh30 · 2017-09-02 04:26 Carefully at second form "Form2" not same the first form. implementation uses   unit1; {$R *.lfm} { TForm2 } ---------------------------------------------------------------------- DavidTh30 · 2017-09-02 04:36 Case 3 multi form or more. At the head of Form1: unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, Forms, Controls, ...,  unit2, unit3; type   { TForm1 }   TForm1 = class(TForm)    .    .    .   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 }  Form2 modify: uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm2 }   TForm2 = class(TForm)     .     .     . TCloseAction);   private     { private declarations }   public     { public declarations }   end; var   Form2: TForm2; implementation uses   unit1,Unit3; {$R *.lfm} { TForm2 }  Form3 modify: uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm3 }   TForm3 = class(TForm)     .     .     .   private     { private declarations }   public     { public declarations }   end; var   Form3: TForm3; implementation uses   unit1, unit2; {$R *.lfm} { TForm3 } ---------------------------------------------------------------------- molly · 2017-09-02 04:39 Quote from: DavidTh30 on September 02, 2017, 06:26:16 amCarefully at second form "Form2" not same the first form. Yes, i know that  That is why i wrote "..waiting to happen". When you have a couple of forms in your project, let's say 25 then do you know from memory which forms used another form inside the interface uses clause ? I guess not, because you start adding other units in the interface section to access other forms and 'out of the blue' you have yourself a circular unit reference, which you would then have to solve. Note that this can happen indirectly as well, so you're basically lost after 4 levels of depth and 4 or 5 forms trying to access other forms (and none of those accessing another form directly). ---------------------------------------------------------------------- molly · 2017-09-02 04:40 Quote from: DavidTh30 on September 02, 2017, 06:36:09 amCase 3 multi form or more. At the head of Form1: unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, Forms, Controls, ...,  unit2, unit3; Please just stop advertising bad coding habits  There is no use at all to do that in the interface section, at least not with the code you showed so far. Carefully read my previous reply and check it yourself and be amazed at how unhelpful the compiler can be in such cases. ---------------------------------------------------------------------- taazz · 2017-09-02 11:12 Quote from: molly on September 02, 2017, 06:39:44 amQuote from: DavidTh30 on September 02, 2017, 06:26:16 amCarefully at second form "Form2" not same the first form. Yes, i know that  That is why i wrote "..waiting to happen". When you have a couple of forms in your project, let's say 25 then do you know from memory which forms used another form inside the interface uses clause ? Yes, the mainform uses all the forms that needs, the rest 24, use none. Quote from: molly on September 02, 2017, 06:39:44 amI guess not, because you start adding other units in the interface section to access other forms and 'out of the blue' you have yourself a circular unit reference, which you would then have to solve. Note that this can happen indirectly as well, so you're basically lost after 4 levels of depth and 4 or 5 forms trying to access other forms (and none of those accessing another form directly). Nope there is only one level of depth and proper coding. If for any reason I fill the need to use any other form then I know my design is wrong. When you write code you must think each unit as an island, it can fire a flare (event and event lists), to inform the islands interested in its affairs that something changed, but it can never reach out and touch other islands with out starting a war. ---------------------------------------------------------------------- Lulu · 2017-09-02 18:58 I read the whole of this subject, it is very interesting, and I learned again today! thank you to all of you ! ---------------------------------------------------------------------- molly · 2017-09-03 02:28 Quote from: taazz on September 02, 2017, 01:12:20 pmYes, the mainform uses all the forms that needs, the rest 24, use none. So you implement all your programs in such simplistic design and every form is only invoked from the main form (and main form only) ? That is perfectly ok if that works for your clients/programs. QuoteNope there is only one level of depth and proper coding. If for any reason I fill the need to use any other form then I know my design is wrong. I'm perfectly ok with the fact that you seem to want to imply that there is something wrong with my design skills, but such remark (icw the one above) seems to suggest to me that there is missing something from yours. No offense. QuoteWhen you write code you must think each unit as an island, it can fire a flare (event and event lists), to inform the islands interested in its affairs that something changed, but it can never reach out and touch other islands with out starting a war. Indeed an island. Self contained and which can be invoked from everywhere in the application at any time (when required). So in order to avoid (future) problems it is much better to avoid including other units inside the interface section of a unit (i have marked the exceptions to that rule). In case you wanted to get your point across that DavidTh30's example works as intended then i guess you have made your point. But that point only works in practice for that particular example of simplistic (linear) design. ---------------------------------------------------------------------- taazz · 2017-09-03 02:47 Quote from: molly on September 03, 2017, 04:28:08 amQuote from: taazz on September 02, 2017, 01:12:20 pmYes, the mainform uses all the forms that needs, the rest 24, use none. So you implement all your programs in such simplistic design and every form is only invoked from the main form (and main form only) ? No every form can be invoked from anywhere in my application it just does not need to know that the action taken will show a form or not. Quote from: molly on September 03, 2017, 04:28:08 am That is perfectly ok if that works for your clients/programs. Yeah I would like to see you try that simplistic design and see how easy it is to accomplish. Quote from: molly on September 03, 2017, 04:28:08 amQuoteNope there is only one level of depth and proper coding. If for any reason I fill the need to use any other form then I know my design is wrong. I'm perfectly ok with the fact that you seem to want to imply that there is something wrong with my design skills, but such remark (icw the one above) seems to suggest to me that there is missing something from yours. No offense. No at all, I can not comment on your design with out knowing your specification and use cases I'm talking about my designs and my designs along, and a simplistic rule that needs to be enforced in all of them. Quote from: molly on September 03, 2017, 04:28:08 amQuoteWhen you write code you must think each unit as an island, it can fire a flare (event and event lists), to inform the islands interested in its affairs that something changed, but it can never reach out and touch other islands with out starting a war. Indeed an island. Self contained and which can be invoked from everywhere in the application at any time (when required). True. Quote from: molly on September 03, 2017, 04:28:08 amSo in order to avoid (future) problems it is much better to avoid including other units inside the interface section of a unit (i have marked the exceptions to that rule). In case you wanted to get your point across that DavidTh30's example works as intended then i guess you have made your point. But that point only works in practice for that particular example of simplistic (linear) design. No I'm just sharing my experience and "rule of contact" nothing more nothing less fill free to ignore me. I always do. ---------------------------------------------------------------------- molly · 2017-09-03 07:45 Quote from: taazz on September 03, 2017, 04:47:05 amNo every form can be invoked from anywhere in my application it just does not need to know that the action taken will show a form or not. Nor does any form unit need to be included. Sorry but i don't see your point in the context of the thread. QuoteYeah I would like to see you try that simplistic design and see how easy it is to accomplish No idea what makes you think i haven't done so already. The original question was not about different design implementations nor were these expressed (until you mentioned it ofc). Quote...and a simplistic rule that needs to be enforced in all of them. And in real life that simply isn't always possible nor desired. Quote... fill free to ignore me. I always do. I value your opinion but so far you haven't been able to convince me at all that i should start adding form-units into my interface section (or implementation section for that matter). ---------------------------------------------------------------------- alaa123456789 · 2020-12-22 16:10 https://youtu.be/1q8G8L4uABQ ---------------------------------------------------------------------- OC DelGuy · 2021-01-03 23:15 Quote from: matthijs on October 29, 2006, 10:20:02 pmYou can not start early enough getting used to using try .. except or try .. finally. I wish there was a video tutorial for this! ---------------------------------------------------------------------- cdbc · 2021-01-04 15:25 Hi I'm with matthijs on this one! You can even make a class function on the form, to do the work... Just a thought  Regards Benny