Recent

Author Topic: Using Objects, which became create in runtine.  (Read 5659 times)

OPFan

  • Newbie
  • Posts: 5
Using Objects, which became create in runtine.
« on: July 30, 2014, 05:12:59 pm »
Hey.

1st of all: Excuse my poor english skills.

I'm currently working on a small programm which shall create a form with objects, as buttons, while runtime.
The big problem for me is: I don't know how to declare the objects, which became create while running the executable.


E.G.

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }


  TForm1 = class(TForm)
    Button1: TButton;
       procedure Button1Click(Sender: TObject);


  {TForm2}
Tform2=class(TForm)
Form2Button:TButton;
//
   
private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  Form2:TForm1;
 Form2Button: TButton;
 
implementation
{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2:= TForm.Create(nil);
  Form2.Width:=219;
  Form2.Height:=373;
  Form2.Top:=223;
  Form2.Left:=110;
  Form2.Caption:='Format ändern';
  Form2Button:=TBUtton.Create(form2);
  Form2ButtonQuelle.parent:=form2;
  Form2Button.Caption:='Form2Button';
  Form2Button.Top:=157;
  Form2Button.Left:=16;
  Form2Button.Height:=24;
  Form2Button.Width:=90;
   Form2.Show;
  Form1.visible:=false;
 end;

{TForm2}
//At this place i want to call a procedure which starts, if somebody clicks Form2Button. + In real i have problems with the declaration of //TForm2 too, because it doesn't becomes created at the programs start. (just added this here to make my plan hopefully more //understandable)

end. 

I'm already excited to see a solution.
Ty all for the time, you spend to help.                                               

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Using Objects, which became create in runtine.
« Reply #1 on: July 30, 2014, 05:21:54 pm »
You could design normally your form and create it at any time on runtime by reallocating Application.CreateForm.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Using Objects, which became create in runtine.
« Reply #2 on: July 30, 2014, 05:27:54 pm »
1) you can't have two forms in the same .pas file.
2) Use the ide specificaly the menu "file\New Form" to create the forms.
3) place any button you want on the the form and double click it to create the event, then write what you want to happen then the button is clicked in there.

To make things a bit easy, after you have created all the forms save the project to make sure that everything is save. Select the form1 and press F11 to go to the source then press alt+F11 to add the Unit2.pas file in the uses clause of unit1.pas. Now you can access the Tform2 class and public variable in unit unit2.pas
You can't use the generic TForm class to create a Tform2 object you need to use TForm2.Create; but if you already have them in different units then you can use the autocreate forms in the project options dialog.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Using Objects, which became create in runtine.
« Reply #3 on: July 30, 2014, 05:53:32 pm »
I wouldn't recommend it, but you could do this, which creates a form inside a form...

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Form2:TForm;
    Form2Button:TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
    procedure ClickMe(Sender: TObject);
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }
procedure TForm1.ClickMe(Sender: TObject);
begin
  ShowMessage('Clicked!');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   If Assigned(Form2) then Exit;
   Form2:=TForm.CreateNew(Nil);
  Form2.Width:=219;
  Form2.Height:=373;
  Form2.Top:=223;
  Form2.Left:=110;
  Form2.Caption:='Format ändern';
  Form2.Parent:=Form1;
  Form2Button:=TBUtton.Create(form2);
  Form2Button.Caption:='Form2Button';
  Form2Button.Top:=157;
  Form2Button.Left:=16;
  Form2Button.Height:=24;
  Form2Button.Width:=90;
  Form2Button.OnClick:=@ClickMe;
  Form2Button.parent:=form2;
  Form2.Show;
end;

end.
                               
« Last Edit: July 30, 2014, 05:56:06 pm by minesadorada »
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Using Objects, which became create in runtine.
« Reply #4 on: July 30, 2014, 06:45:38 pm »
This method is recommended.
Code: [Select]
unit mainDynamicForm;

{$mode objfpc}{$H+}

interface

uses
  Classes, Forms, StdCtrls, ButtonPanel;

type

  { TForm1 }

  TForm1 = class(TForm)
    BCreateDynamicForm: TButton;
    procedure BCreateDynamicFormClick(Sender: TObject);
  private
    FDynamic: TForm;
    function CreateDynamicForm: TForm;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.BCreateDynamicFormClick(Sender: TObject);
begin
  FDynamic:=CreateDynamicForm;
  FDynamic.ShowModal;
end;

function TForm1.CreateDynamicForm: TForm;
var
  frm: TForm;
  cb: TCheckBox;
  lbl: TLabel;
  bPanel: TButtonPanel;
begin
  frm:=TForm.CreateNew(Application);
  frm.Position:=poMainFormCenter;

  bPanel:=TButtonPanel.Create(frm);
  bPanel.Parent:=frm;
  bPanel.ShowButtons:=[pbOK, pbCancel];

  lbl:=TLabel.Create(frm);
  lbl.Caption:='Example Label';
  lbl.Left:=10;
  lbl.Top:=10;
  lbl.Parent:=frm;

  cb:=TCheckBox.Create(frm);
  cb.Caption:='Example checkbox';
  cb.Left:=10;
  cb.Top:=lbl.ClientRect.Bottom + 10;
  cb.Parent:=frm;

  Result:=frm;
end;

end.


OPFan

  • Newbie
  • Posts: 5
Re: Using Objects, which became create in runtine.
« Reply #5 on: July 31, 2014, 09:32:32 am »
1) you can't have two forms in the same .pas file.
2) Use the ide specificaly the menu "file\New Form" to create the forms.
3) place any button you want on the the form and double click it to create the event, then write what you want to happen then the button is clicked in there.

To make things a bit easy, after you have created all the forms save the project to make sure that everything is save. Select the form1 and press F11 to go to the source then press alt+F11 to add the Unit2.pas file in the uses clause of unit1.pas. Now you can access the Tform2 class and public variable in unit unit2.pas
You can't use the generic TForm class to create a Tform2 object you need to use TForm2.Create; but if you already have them in different units then you can use the autocreate forms in the project options dialog.

TY.
But i don't understand how to call form2 then.
#1 It shouldn't be visible at the programs start
#2 WHen i CLick on a special button, i want to call form 2

I connected unit2 with 1, as you described.
Then I tried: Form2.Show or Form2.visible:=True
But i don't see any effects. I probably missed something.

PS: DOn'T wonder, I'm a new to the topic: use more than one unit and to lazarus

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Using Objects, which became create in runtine.
« Reply #6 on: July 31, 2014, 09:58:22 am »
Code: [Select]
var
  vForm2 : TForm2;
begin
  vform2 := TForm2.Create(nil);
  try
    vForm2.ShowModal;
  finally
    vform2.Free;
  end;
end;

The above code will show form2 and lock form1 until you close form2. you can have both forms active at the same by using the show method but you need to rewrite the code like this

Code: [Select]
begin
  with TForm2.Create(Application) do show;
end;

or
Code: [Select]
begin
  if not assigned(Form2) then form2 := TForm2.Create(application);
  Form2.Show;
end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

OPFan

  • Newbie
  • Posts: 5
Re: Using Objects, which became create in runtine.
« Reply #7 on: July 31, 2014, 10:21:42 am »
Code: [Select]
var
  vForm2 : TForm2;
begin
  vform2 := TForm2.Create(nil);
  try
    vForm2.ShowModal;
  finally
    vform2.Free;
  end;
end;

The above code will show form2 and lock form1 until you close form2. you can have both forms active at the same by using the show method but you need to rewrite the code like this

Code: [Select]
begin
  with TForm2.Create(Application) do show;
end;

or
Code: [Select]
begin
  if not assigned(Form2) then form2 := TForm2.Create(application);
  Form2.Show;
end;

Ok. Thank you. That's exactly what i wanted and needed.

OPFan

  • Newbie
  • Posts: 5
Re: Using Objects, which became create in runtine.
« Reply #8 on: July 31, 2014, 11:30:00 am »
Finally, I have another question.

How can I connect the declaration of a variable with another unit.

E.G.
On Unit 1:

Q:=1;

On Unit 2:

Button2Click...
Q:=0

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Using Objects, which became create in runtine.
« Reply #9 on: July 31, 2014, 11:43:36 am »
Make your variable in form1 public.
Code: [Select]
type Tform1 = class(TForm)
.....
public
  Q : integer;
end;
Put your form1 into the uses of your second form.
Call the variable as Form1.Q

Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018