Creating draggable controls at run-time lazarus / 31-LCL ---------------------------------------------------------------------- Vodnik · 2020-01-08 08:14 Hello, Up to now I had to deal only with static controls, hiding and disabling them, etc. Now customer asks for some kind of real-time report designer. The idea is that user can select some controls (e.g. TLabel) from the menu, place them on a form, change their size, font, color, etc. and link to a predefined variables from a list. Such freely designed form, when activated, should show how the selected variables change in real-time (e.g. number of calls, number of operators, etc.) Question is: is there a necessary functionality available in Lazarus to create, edit and drag controls at run-time? PS  I have read about FPReport Designer, seems it is a bit different thing. ---------------------------------------------------------------------- balazsszekely · 2020-01-08 08:48 @Vodnik You can create you're own custom control, but it's not a trivial task. A few years ago I converted the "SizeControl.pas" delphi unit(author: Angus Johnson) to lazarus. It was requested by a forum user, IIRC it only worked on windows. Please download the attached project and run a few test yourself. ---------------------------------------------------------------------- wp · 2020-01-08 09:38 A form designer component is contained in the JVCL library and has been ported to Lazarus. JVCLLaz is available via Online Package Manager. Run the demo JvDesigner in folder examples of the JVCLLaz installation. If you plan to use additional components for this designer you should search the forum where I explained the steps not too long ago; instructions are also given in the header of the main form unit of the demo. ---------------------------------------------------------------------- Vodnik · 2020-01-08 18:41 As I understand, the features I am asking about are not standard for Lazarus controls. @Getmem, thank you for the SizeControl unit, I have just tested it. Controls resize and dragging work fine. What about creating controls at run-time? Is it possible in principle? @wp, thanks, I learned a lot of new from your answer, just investigating. ---------------------------------------------------------------------- sash · 2020-01-08 22:10 Quote from: Vodnik on January 08, 2020, 07:41:46 pmWhat about creating controls at run-time? Is it possible in principleThis is the easiest partCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i MyControl := TLabel.Create(OwnerForm); // ownershipMyControl.Parent := (SomePanelOrWhatever); //geometry ---------------------------------------------------------------------- Vodnik · 2020-01-09 14:47 @Sash, thanks, but I didn't completely catch the idea. The following code cause no error, but MyLabel is not displayed on a form: 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 unit Unit1;{$mode objfpc}{$H+}interfaceuses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;type  { TForm1 }  TForm1 = class(TForm)    Label1: TLabel;  private  public  end;var  Form1: TForm1;implementationvar MyLabel: TLabel;beginMyLabel := TLabel.create(Form1);MyLabel.Parent := Form1;MyLabel.Left:=100;MyLabel.Top:=100;{$R *.lfm}end. ---------------------------------------------------------------------- wp · 2020-01-09 15:45 No this is not the way to do it. Your code between "begin" and "end" is in the initialization section of the unit - it is executed when the unit is loaded, at this moment the form does not yet exist. You can be lucky that this did not even crash the program. This is the correct way: Find the event "OnCreate" of the form and click on the ... button. Put your code in the code editor into the code skeleton which is created automatically.The "TForm1" added in front of the name of the event handler indicates that this code is a "method" of TForm1, i.e. it can only be executed within an instance of the class TForm1. Therefore, refering to the form by its name "Form1" in "MyLabel := TLabel.Create(Form1)" is a bad idea - this will break the code when you somehow name the instance differently. It is much more versatile when you refer to the form by its general name "self".Declare MyLabel as a private field of TForm1.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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  { TForm1 }  TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  // handler for the OnCreate event of the form  private    MyLabel: TLabel;      // <--- the label "belongs" to the form  public  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } // This is executed when the form is created.procedure TForm1.FormCreate(Sender: TObject);begin  MyLabel := TLabel.Create(Self);   // Do not use the form variable name here. Use "self" instead of "Form1"  MyLabel.Parent := self;   // dto.  MyLabel.Left := 100;  MyLabel.Top := 100;  MyLabel.Caption := 'Hello world';   // Set the caption of the label - an empty string cannot be seen.end; end. Complete compilable project in the attachment ---------------------------------------------------------------------- Vodnik · 2020-01-09 21:19 Wp, great respect for your code, explanation and patience... I modified the code to allow the application user to add some Labels when the form is already created: 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 unit Unit1;{$mode objfpc}{$H+}interfaceuses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Menus;type  { TForm1 }  TForm1 = class(TForm)    MainMenu1: TMainMenu;    MenuItem1: TMenuItem;    procedure MenuItem1Click(Sender: TObject);  private    MyLabels: array[1..10] of TLabel;  public  end;var  Form1: TForm1;  I: integer = 1;implementation{$R *.lfm}{ TForm1 }procedure TForm1.MenuItem1Click(Sender: TObject);begin  if I>10 then exit;  MyLabels[I]:=TLabel.Create(Self);  MyLabels[I].Parent:=self;  MyLabels[I].Left := 20;  MyLabels[I].Top := 20*I;  MyLabels[I].Caption := 'Hello world '+IntToStr(I);  Inc(I);end;end. ---------------------------------------------------------------------- soerensen3 · 2020-01-10 15:46 You can also have a look at pl_ExDesign. It does what you want, but I'm not sure about the license (obviously the author is from the codetyphon project. You can download it via the online package manager. There are also examples include.