* * *

Author Topic: Using multiple forms  (Read 14700 times)

dani.user

  • Newbie
  • Posts: 1
Using multiple forms
« on: October 28, 2006, 11:01:21 am »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1889
RE: Using multiple forms
« Reply #1 on: October 28, 2006, 11:42:20 am »
File -> New Form

then in Unit1
Code: [Select]
...

implementation

uses Unit2;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2.ShowModal;
end;  

...

matthijs

  • Hero Member
  • *****
  • Posts: 537
RE: Using multiple forms
« Reply #2 on: October 29, 2006, 11:52:10 am »
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.
What's in a sig? Would my posting look less if it didnot have a sig? (Free after William S.) :)

:( Why cannot I upload my own Avatar? :(

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1889
Re: RE: Using multiple forms
« Reply #3 on: October 29, 2006, 01:02:48 pm »
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

  • Sr. Member
  • ****
  • Posts: 335
RE: Re: RE: Using multiple forms
« Reply #4 on: October 29, 2006, 02:04:08 pm »
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 :D

matthijs

  • Hero Member
  • *****
  • Posts: 537
Re: RE: Using multiple forms
« Reply #5 on: October 29, 2006, 06:09:18 pm »
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. :)
What's in a sig? Would my posting look less if it didnot have a sig? (Free after William S.) :)

:( Why cannot I upload my own Avatar? :(

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1889
Re: RE: Using multiple forms
« Reply #6 on: October 29, 2006, 07:52:33 pm »
Quote
I 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

  • Hero Member
  • *****
  • Posts: 537
Re: RE: Using multiple forms
« Reply #7 on: October 29, 2006, 10:20:02 pm »
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.
What's in a sig? Would my posting look less if it didnot have a sig? (Free after William S.) :)

:( Why cannot I upload my own Avatar? :(

RudieD

  • Full Member
  • ***
  • Posts: 234
RE: Re: RE: Using multiple forms
« Reply #8 on: November 02, 2006, 07:42:10 pm »
I agree with Matthijs, if you do it, do it right. I also usualy check if <TForm2.OnClose> if the instance is the global instance and then set Form2 = nil. Bad habbits die hard !
The FRED Trainer. (Training FRED with Lazarus/FPC)

mrbruno7447

  • Newbie
  • Posts: 1
Re: RE: Using multiple forms
« Reply #9 on: July 03, 2014, 03:10:10 am »
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?

kapibara

  • Sr. Member
  • ****
  • Posts: 460
Re: Using multiple forms
« Reply #10 on: July 03, 2014, 06:05:42 am »
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.

Lazarus trunk / fpc 3.0 / Debian Stretch 64-bit

DavidTh30

  • New member
  • *
  • Posts: 5
Re: Using multiple forms
« Reply #11 on: September 02, 2017, 04:02:18 am »
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.
 8-) 8-) 8-) 8-)

molly

  • Hero Member
  • *****
  • Posts: 1799
Re: Using multiple forms
« Reply #12 on: September 02, 2017, 04:18:53 am »
I alway Do some thing like this.

Code: Pascal  [Select]
  1. interface
  2. uses
  3.   Classes, SysUtils, Forms, Controls, ...,  unit2;
  4.  

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

  • New member
  • *
  • Posts: 5
Re: Using multiple forms
« Reply #13 on: September 02, 2017, 06:26:16 am »
Carefully at second form "Form2" not same the first form.

implementation
uses
  unit1;
{$R *.lfm}
{ TForm2 }

DavidTh30

  • New member
  • *
  • Posts: 5
Re: Using multiple forms
« Reply #14 on: September 02, 2017, 06:36:09 am »
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 }
« Last Edit: September 02, 2017, 06:39:58 am by DavidTh30 »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus