Recent

Author Topic: Curious about how TForm's instant instantiate it's field member.  (Read 2027 times)

LittleRabbit

  • Newbie
  • Posts: 2
Hello All.

I'm super newbie, teaching myself programming in the hope of to be able to build an application for my personal use.
I'm studying free pascal/ lazarus.

Starting with standard lazarus application project, the lazarus automatically generated code skeleton, and i  added a button to the form, then the code looked like this
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.   private
  17.  
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. end.
  30.  

My questions are :
1. Where did the constructor of the Form1 is called? , in Application.CreateForm(TForm1, Form1); ?
2. How constructor of Form1 object call constructor of Button1 without the need to
override the constructor of TForm?

Thank you.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Curious about how TForm's instant instantiate it's field member.
« Reply #1 on: June 26, 2021, 07:37:35 am »
1. Where did the constructor of the Form1 is called? , in Application.CreateForm(TForm1, Form1); ?

That's not the constructor per se but yes, it's where the form's Create() is called among other things ... which lead us to:

Quote
2. How constructor of Form1 object call constructor of Button1 without the need to
override the constructor of TForm?

It's not the "constructor" of the form but the CreateForm() method of TApplication. Very, very basically, after some convolutions and among other things, the form resource (let's say, the .lfm embedded in the program) is read, the form's properties set from it, controls and components (classes) read, instances created and properties set from it, etc.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

LittleRabbit

  • Newbie
  • Posts: 2
Re: Curious about how TForm's instant instantiate it's field member.
« Reply #2 on: June 26, 2021, 08:34:50 am »
Thank you very very much , lucama.
Lazarus team is very clever.

GUI is so complex.  %)

 

TinyPortal © 2005-2018