Best way to dynamically create main form lazarus / 31-LCL ---------------------------------------------------------------------- simone · 2019-06-22 15:44 Dear all, I'm investigating the best way to dynamically create main forms of GUI applications. My purpose is to build a code skeleton as basis of rad tool I'm building. About this topic I read some interesting wikis and threads. As far as I understood, there are (at least), two approaches: Method #1: (TForm constructor overriding and using CreateNew method) 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 program project1;{$mode objfpc}{$H+} uses  Interfaces, Forms, Classes, SysUtils, Controls, Graphics, Dialogs, StdCtrls; type  TMainForm=class(TForm)                                                                   procedure ButtonOnClick(Sender : TObject);    constructor Create(TheOwner: TComponent); override;  end; var  Form : TMainForm;  Button : TButton; procedure TMainForm.ButtonOnClick(Sender: TObject);begin  ShowMessage('Ok');end; constructor TMainForm.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);                        Button:=TButton.Create(Form);  with Button do    begin      Parent:=Form;      Caption:='Press';      Left:=50;      Top:=50;    Width:=80;      Height:=30;      OnClick:=@Form.ButtonOnClick;    end;end; begin  Application.Scaled:=True;  Application.Initialize;  Application.CreateForm(TMainForm,Form);  Application.Run;end.  Method #2: (without overriding TForm contructor and using Create method) 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 program project1;{$mode objfpc}{$H+} uses  Interfaces, Forms, Classes, SysUtils, Controls, Graphics, Dialogs, StdCtrls; type  TMainForm=class(TForm)    procedure ButtonOnClick(Sender : TObject);  end; var  Form : TMainForm;  Button : TButton; procedure TMainForm.ButtonOnClick(Sender: TObject);begin  ShowMessage('Ok');end; begin  Application.Scaled:=True;  Application.Initialize;   Form:=TMainForm.Create(Application);  Form.Show;   Button:=TButton.Create(Form);  with Button do    begin    Parent:=Form;      Caption:='Press';      Left:=50;      Top:=50;    Width:=80;      Height:=30;      OnClick:=@Form.ButtonOnClick;    end;   Application.Run;end. Which of the two approaches is the most convenient? Thanks in advance. ---------------------------------------------------------------------- RAW · 2019-06-22 16:47 I guess the best way is to use PROJECT -> NEW PROJECT -> APPLICATION to go the standard unit-way.  I cannot see where in the first example CREATENEW is used and the second example won't close the app when the window is closed and the user won't see any taskbar button. EDIT: the second example needs at least 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 program project1;{$mode objfpc}{$H+} uses  Interfaces, Forms, Classes, SysUtils, Controls, Graphics, Dialogs, StdCtrls; type  TMainForm = class(TForm)    procedure ButtonOnClick(Sender : TObject);    procedure OnMyClose(Sender: TObject; var CloseAction: TCloseAction);  end; procedure TMainForm.ButtonOnClick(Sender: TObject);begin  ShowMessage('Ok');end; procedure TMainForm.OnMyClose(Sender: TObject; var CloseAction: TCloseAction);begin  Application.Terminate; // Or OnDestroyend; procedure StartApp;var Form  : TMainForm; Button: TButton;begin  Application.Initialize;    Form        :=TMainForm.Create(Application);   Form.OnClose:= @Form.OnMyClose;   Form.Show;     Button:=TButton.Create(Form);     with Button do      begin        Parent:=Form;        Caption:='Press';        Left:=50;      Top:=50;        Width:=80;        Height:=30;        OnClick:=@Form.ButtonOnClick;      end;  Application.Run;end; begin  StartApp;end. ---------------------------------------------------------------------- simone · 2019-06-22 18:43 Quote from: RAW on June 22, 2019, 06:47:12 pmI guess the best way is to use PROJECT -> NEW PROJECT -> APPLICATION to go the standard unit- way.  Yes, I know, but in my context I'm building a tool that generates code, thus I can't use the standard RAD way. I simply want to know the best practice to create main form with a programmatically approach. Quote from: RAW on June 22, 2019, 06:47:12 pmI cannot see where in the first example CREATENEW is used My mistake writing the post… I meant 'CreateForm'... Quote from: RAW on June 22, 2019, 06:47:12 pmand the second example won't close the app when the window is closed and the user won't see any taskbar button. You are right… I presume the first method is preferable... Thanks for explanations ---------------------------------------------------------------------- SymbolicFrank · 2019-06-22 20:32 The simplest way is to just create and show them. ---------------------------------------------------------------------- RAW · 2019-06-22 21:00 QuoteThe simplest way is to just create and show them. This is easy to do and works, BUT not very nice !!!  A second taskbar-button appears if I click on minimize 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 program Project1;{$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,  {$ENDIF}{$ENDIF}   Interfaces,   Forms, Classes, StdCtrls, Dialogs;  {$R *.res} procedure ButtonClick(Self: TForm; Sender: TObject);begin  ShowMessage('Hello World');end; procedure Run;var wnd: TForm; btn: TButton; clk: TNotifyEvent;begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;   wnd              := TForm.Create(Application);  wnd.SetBounds    (50, 50, 300, 200);  wnd.ShowInTaskBar:= stAlways; // main project gets also a taskbar button  wnd.Caption      := 'MY PRG';   TMethod(clk).Code:= @ButtonClick;  TMethod(clk).Data:= wnd;   btn          := TButton.Create(wnd);  btn.Caption  := 'CLICK ME';  btn.SetBounds(0, 0, 100, 50);  btn.OnClick  := clk;  btn.Parent   := wnd;   wnd.ShowModal;end; begin  Run;end. You can use only one procedure for everything if you like // no units, no type ...  There is still the taskbar button thing ... maybe a closer look into Application.CreateForm will help ? It would be easier with the old OBJECT type or RECORD (advanced), but I guess nested methods are forbidden.  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 procedure ShowMainWnd(x, y, w, h: Integer);var       // main form as one proc, no units, no type, no other stuff wnd : TForm; btn1, btn2: TButton; clk1, clk2: TNotifyEvent;  // all events inside the proc procedure Button1Click(Self: TForm; Sender: TObject); begin   ShowMessage('Button 1'); end;  procedure Button2Click(Self: TForm; Sender: TObject); begin   ShowMessage('Button 2'); end; begin  wnd        := TForm.Create(Application);  wnd.SetBounds(x, y, w, h);  wnd.Caption  := 'My Window Proc';   TMethod(clk1).Code:= @Button1Click;  TMethod(clk1).Data:= wnd;   btn1          := TButton.Create(wnd);  btn1.Caption  := 'Button 1';  btn1.SetBounds(0, 0, 100, 50);  btn1.OnClick  := clk1;  btn1.Parent   := wnd;   TMethod(clk2).Code:= @Button2Click;  TMethod(clk2).Data:= wnd;   btn2        := TButton.Create(wnd);  btn2.Caption  := 'Button 2';  btn2.SetBounds(0, 80, 100, 50);  btn2.OnClick  := clk2;  btn2.Parent := wnd;   wnd.ShowModal;end; 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 begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;  ShowMainWnd(100, 100, 400, 300);end. This is working a lot better ...  not perfect though if you use minimize, but only one button ! 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 begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;  Application.TaskBarBehavior:= tbSingleButton;  ShowMainWnd(0, 0, 400, 300);end. ---------------------------------------------------------------------- lucamar · 2019-06-22 22:50 In your first example--if you're going that route--please, don't reference a particular instance from inside the class! Your constructor, for example, should look 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 constructor TMainForm.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);                         Button:=TButton.Create(Form);  with Button do    begin      Parent := Self; {*NOT* Parent := form}      Caption:='Press';      Left:=50;    Top:=50;      Width:=80;      Height:=30;      OnClick := @ButtonOnClick; {*NOT* @Form.ButtonOnClick;}    end;end; Otherwise, only that particular object instance will work, not any other that may be created dynamically; i.e. if someone clicks the button in say AnotherForm, it will act as if the button in Form had been clicked. Which, I assume, is not what you want, is it? ---------------------------------------------------------------------- RAW · 2019-06-23 00:08 This works too ...  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 program Project1;{$mode objfpc}{$H+} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}   cthreads,  {$ENDIF}{$ENDIF}   Interfaces,   Forms, Classes, StdCtrls, Dialogs;  {$R *.res} procedure ShowMainWnd(x, y, w, h: Integer);var     // main form as one proc, no units, no type... wnd : TForm; btn1, btn2: TButton; clk1, clk2: TNotifyEvent;  // all events inside the proc procedure Button1Click(Sender: TObject); begin   ShowMessage('Button 1'); end;  procedure Button2Click(Sender: TObject); begin   ShowMessage('Button 2'); end; begin  wnd          := TForm.Create(Application);  wnd.SetBounds(x, y, w, h);  wnd.Caption  := 'My Window Proc';   TMethod(clk1).Code:= @Button1Click;  TMethod(clk2).Code:= @Button2Click;   btn1          := TButton.Create(wnd);  btn1.Caption  := 'Button 1';  btn1.SetBounds(0, 0, 100, 50);  btn1.OnClick  := clk1;  btn1.Parent   := wnd;   btn2        := TButton.Create(wnd);  btn2.Caption  := 'Button 2';  btn2.SetBounds(0, 80, 100, 50);  btn2.OnClick  := clk2;  btn2.Parent := wnd;   wnd.ShowModal;end; begin  RequireDerivedFormResource:=True;  Application.Scaled:=True;  Application.Initialize;  Application.TaskBarBehavior:= tbSingleButton;  ShowMainWnd(0, 0, 400, 300);end. ---------------------------------------------------------------------- simone · 2019-06-23 08:56 Quote from: lucamar on June 23, 2019, 12:50:31 amIn your first example --if you're going that route--please, don't reference a particular instance from inside the class! My mistake, due to cut&paste of that code from 'method #2', where instance 'form' is necessary, because here we are inside begin...end., outside the method of class. Quote from: RAW on June 23, 2019, 02:08:54 amThis works too ...  Very interesting 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   TMethod(clk1).Code:= @Button1Click;  TMethod(clk2).Code:= @Button2Click; Thanks...