Create components and order them at runtime lazarus / 31-LCL ---------------------------------------------------------------------- valter.home · 2020-09-22 16:26 Hi everyone. it seems nonsense but I can't figure it out. I need to create components at runtime but I can't place them correctly. I should have in order: a TLabel, a TMemo, a TLabel and a TFilenameEdit. I was able to get this sequence only by using a TPanel for each component but it seems a waste to me. I'll give you a simple code example: 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 Caption1:= TLabel.Create(PanelContainer);Caption1.Parent:=PanelContainer;Caption1 .Caption:='Caption 1';Caption1.AutoSize:=True;Caption1.Align:=alBottom; Memo1 := TMemo.Cr eate(PanelContainer);Memo1.Parent:=PanelContainer;Memo1.Height:=40;Mem o1.Align:=alBottom;Memo1.ScrollBars:=ssAutoBoth;Memo1.Clear; Caption2: = TLabel.Create(PanelContainer);Caption2.Parent:=PanelContainer;Captio n2.Caption:='Caption 2';Caption2.AutoSize:=True;Caption2.Align:=alBottom; LoadFile := TFile nameEdit.Create(PanelContainer);LoadFile.Parent:=PanelContainer;LoadFi le.Align:=alBottom;LoadFile.Text:=String.Empty; Of course this is just an example, I tried to modify it several times by setting for example the Top parameter of a component below the Top + Height of the above component and then setting Align to AlBottom hoping that it would remain below the one above. It happens that the TLabels gather together and do not hold the position of creation. These components are located on one side of a TSplitter so they must also resize when it is moved. In short, have you ever found yourself in this situation and how did you solve it? ---------------------------------------------------------------------- Handoko · 2020-09-22 16:39 Quote from: valter.home on September 22, 2020, 06:26:51 pmIt happens that the TLabels gather together and do not hold the position of creation. If I understood what you want correctly, I believe you should set the values of BolderSpacing property. Quote from: valter.home on September 22, 2020, 06:26:51 pmThese components are located on one side of a TSplitter so they must also resize when it is moved. I believe it can be achieve by 2 panels + a splitter, for example the first panel is set to Align := alLeft, then a spliter which also Align := alLeft and the last panel should be Align := alClient; ---------------------------------------------------------------------- valter.home · 2020-09-22 17:07 QuoteIf I understood what you want correctly, I believe you should set the values of BolderSpacing property. Hi, thanks for your reply. I probably explained myself badly. What happens is that I create the 4 components mentioned above at runtime but they do not respect the creation sequence. TLabel then TMemo then TLabel and finally TFilenameEdit. Each of them must be on top of the other, for this use Align: = AlBottom. Instead I get TLabel, TLabel, TMemo and TFilenameEdit or TLabel, TFilenameEdit, TLabel and TMemo. It appears that the components are placed randomly. QuoteI believe it can be achieve by 2 panels + a splitter This part works well, I pointed out that there is a TSplitter to explain why I use Align for components. ---------------------------------------------------------------------- Blaazen · 2020-09-22 17:16 Use Anchors instead of Align. You will have better control over your components. ---------------------------------------------------------------------- Handoko · 2020-09-22 17:21 It works on my test on Linux GTK2. Download and run my code and see is that what you want. 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, ExtCtrls, StdCtrls, EditBtn; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    PanelContainer: TPanel;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  Caption1, Caption2: TLabel;  Memo1: TMemo;  LoadFile: TFilenameEdit;begin  Caption1:= TLabel.Create(PanelContainer);  Caption1.Parent:=PanelContainer;  Caption1.Caption:='Caption 1';  Caption1.AutoSize:=True;  Caption1.Align:=alBottom;   Memo1 := TMemo.Create(PanelContainer);  Memo1.Parent:=PanelContainer;  Memo1.Height:=40;  Memo1.Align:=alBottom;  Memo1.ScrollBars:=ssAutoBoth;  Memo1.Clear;   Caption2:= TLabel.Create(PanelContainer);  Caption2.Parent:=PanelContainer;  Caption2.Caption:='Caption 2';  Caption2.AutoSize:=True;  Caption2.Align:=alBottom;   LoadFile := TFilenameEdit.Create(PanelContainer);  LoadFile.Parent:=PanelContainer;  LoadFile.Align:=alBottom;  LoadFile.Text:=String.Empty;end; procedure TForm1.FormCreate(Sender: TObject);begin  PanelContainer.Anchors := [akTop, akBottom, akLeft, akRight];end; end. ---------------------------------------------------------------------- valter.home · 2020-09-22 19:04 Your exact same code with Lazarus 2.0.10 under Windows 10 places the components in this order from the top: Caption1, LoadFile, Caption2, Memo. I do not understand. ---------------------------------------------------------------------- howardpc · 2020-09-22 19:48 As Blaazen wrote, anchors give you the fine control you need. 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 TForm1.Button1Click(Sender: TObject);var  Caption1, Caption2: TLabel;  Memo1: TMemo;  LoadFile: TFilenameEdit;begin  Caption1 := TLabel.Create(PanelContainer);  Caption1.Caption := 'Caption 1';  Caption1.AnchorSideLeft.Control := PanelContainer;  Caption1.AnchorSideTop.Control := PanelContainer;  Caption1.Parent:=PanelContainer;   Memo1 := TMemo.Create(PanelContainer);  Memo1.Height := 40;  Memo1.ScrollBars := ssAutoBoth;  Memo1.Clear;  Memo1.Anchors := [akLeft, akTop, akRight];  Memo1.AnchorSideTop.Control := Caption1;  Memo1.AnchorSideTop.Side := asrBottom;  Memo1.AnchorSideLeft.Control := PanelContainer;  Memo1.AnchorSideRight.Control := PanelContainer;  Memo1.AnchorSideRight.Side := asrRight;  Memo1.Parent := PanelContainer;   Caption2:= TLabel.Create(PanelContainer);  Caption2.Caption:='Caption 2';  Caption2.AnchorSideLeft.Control := PanelContainer;  Caption2.AnchorSideTop.Control := Memo1;  Caption2.AnchorSideTop.Side := asrBottom;  Caption2.Parent:=PanelContainer;   LoadFile := TFilenameEdit.Create(PanelContainer);  LoadFile.Anchors := [akLeft, akTop, akRight];  LoadFile.AnchorSideLeft.Control := PanelContainer;  LoadFile.AnchorSideTop.Control := Caption2;  LoadFile.AnchorSideTop.Side := asrBottom;  LoadFile.AnchorSideRight.Control := PanelContainer;  LoadFile.AnchorSideRight.Side := asrRight;  LoadFile.Text:=String.Empty;  LoadFile.Parent:=PanelContainer;end;You probably will want additional settings for BorderSpacing properties to improve the layout, as Handoko suggests. ---------------------------------------------------------------------- Handoko · 2020-09-22 20:03 Quote from: valter.home on September 22, 2020, 09:04:03 pmYour exact same code with Lazarus 2.0.10 under Windows 10 places the components in this order from the top: Caption1, LoadFile, Caption2, Memo. I do not understand. I use Ubuntu Mate Lazarus 2.0.10 GTK2, I did not test the code on Windows. If if behaves differently then it should be OS related issue. You can submit the issue to the bugtracker: http://bugs.freepascal.org/view_all_bug_page.php?project_id=1 ---------------------------------------------------------------------- valter.home · 2020-09-22 20:23 QuoteAs Blaazen wrote, anchors give you the fine control you need. Thanks so much. I was just working on Blaazen's suggestion but coming from Unity I was browsing through the various options, you really helped me by saving me a lot of time. Thanks again. ---------------------------------------------------------------------- valter.home · 2020-09-22 20:54 QuoteYou can submit the issue to the bugtracker Yes, Howardpc's code definitely solves the problem but it's a shame not to be able to insert components in sequence simply by creating them. I tried to replace the 4 components with four TPanels. They are shown  with this sequence: 1 - 4 - 3 - 2. ---------------------------------------------------------------------- jamie · 2020-09-23 00:34 You could always look at TFowPanel control. ---------------------------------------------------------------------- eny · 2020-09-23 08:01 Quote from: valter.home on September 22, 2020, 06:26:51 pmIn short, have you ever found yourself in this situation and how did you solve it? Unfortunately, yes. And you will be in a whole new world of pain when you start hiding and unhiding components and want to retain a certain ordering Anchors, as suggested by Blaazen and explained by Howardpc, work quite well in Lazarus. ---------------------------------------------------------------------- valter.home · 2020-09-23 10:26 QuoteYou could always look at TFowPanel control. I did not know this component, it is great for maintaining the order of the components within it but I think I understand that the Align and Anchors properties are ignored. In this case, by resizing the panel they would not remain anchored to the right. In any case, thanks for letting me discover an interesting component. I should take some time and go through all them. ---------------------------------------------------------------------- JuhaManninen · 2020-09-23 11:10 Yes, anchors is the way to go. The Lazarus / LCL layout system is vastly enhanced from Delphi's simple system with Align property. The AnchorSide... and BorderSpacing properties require some learning and they may not appear intuitive always. A good strategy is to build a layout in form designer. Then if you must create the controls + layout in code, just copy the relevant parts from the .lfm file, replace '=' with ':=' and add ';' to the end. I actually just did that for the file / dependency properties GUI in the PackageEditor window, but did not commit yet. In designer the controls were on top of each other and difficult to maintain. The code will be partly reused for ProjectInspector window later. See: 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   // file properties  // ---------------  CallRegisterProcCheckBox := TCheckBox.Create(fOwner);  CallRegisterProcCheckBox.Parent := fOwner;  CallRegisterProcCheckBox.Left := 6;  CallRegisterProcCheckBox.Top := 0;  CallRegisterProcCheckBox.Width := 185;  CallRegisterProcCheckBox.ShowHint := True;  CallRegisterProcCheckBox.TabOrder := 0;  CallRegisterProcCheckBox.Caption := lisPckEditRegisterUnit;  CallRegisterProcCheckBox.Hint := Format(lisPckEditCallRegisterProcedureOfSelectedUnit, ['"', '"']);   AddToUsesPkgSectionCheckBox := TCheckBox.Create(fOwner);  AddToUsesPkgSectionCheckBox.Parent := fOwner;  AddToUsesPkgSectionCheckBox.AnchorSideLeft.Control := CallRegisterProcCheckBox;  AddToUsesPkgSectionCheckBox.AnchorSideLeft.Side := asrBottom;  AddToUsesPkgSectionCheckBox.Left := 195;  AddToUsesPkgSectionCheckBox.Top := 0;  AddToUsesPkgSectionCheckBox.Width := 222;  AddToUsesPkgSectionCheckBox.BorderSpacing.Left := 10;  AddToUsesPkgSectionCheckBox.ShowHint := True;  AddToUsesPkgSectionCheckBox.TabOrder := 1;  AddToUsesPkgSectionCheckBox.Caption := lisPkgMangUseUnit;  AddToUsesPkgSectionCheckBox.Hint := lisPkgMangAddUnitToUsesClause;   RegisteredPluginsGroupBox := TGroupBox.Create(fOwner);  RegisteredPluginsGroupBox.Parent := fOwner;  RegisteredPluginsGroupBox.Left := 3;  RegisteredPluginsGroupBox.Height := 165;  RegisteredPluginsGroupBox.Top := 27;  RegisteredPluginsGroupBox.Width := 452;  RegisteredPluginsGroupBox.Align := alBottom;  RegisteredPluginsGroupBox.Anchors := [akTop, akLeft, akRight, akBottom];  RegisteredPluginsGroupBox.BorderSpacing.Top := 6;  RegisteredPluginsGroupBox.TabOrder := 7;  RegisteredPluginsGroupBox.Caption := lisPckEditRegisteredPlugins;   RegisteredListBox := TListBox.Create(fOwner);  RegisteredListBox.Parent := RegisteredPluginsGroupBox;  RegisteredListBox.Align := alClient;  RegisteredListBox.ScrollWidth := 448;  RegisteredListBox.Style := lbOwnerDrawFixed;  RegisteredListBox.TabOrder := 0;  RegisteredListBox.ItemHeight := ComponentPaletteImageHeight;   // dependency properties  // ---------------------  UseMinVersionCheckBox := TCheckBox.Create(fOwner);  UseMinVersionCheckBox.Parent := fOwner;  UseMinVersionCheckBox.AnchorSideTop.Control := MinVersionEdit;  UseMinVersionCheckBox.AnchorSideTop.Side := asrCenter;  UseMinVersionCheckBox.Left := 6;  UseMinVersionCheckBox.Top := 6;  UseMinVersionCheckBox.Width := 179;  UseMinVersionCheckBox.TabOrder := 2;  UseMinVersionCheckBox.Caption := lisPckEditMinimumVersion;  UseMinVersionCheckBox.OnChange := @UseMinVersionCheckBoxChange;   MinVersionEdit := TEdit.Create(fOwner);  MinVersionEdit.Parent := fOwner;  MinVersionEdit.AnchorSideLeft.Control := UseMinVersionCheckBox;  MinVersionEdit.AnchorSideLeft.Side := asrBottom;  MinVersionEdit.Left := 201;  MinVersionEdit.Top := 0;  MinVersionEdit.Width := 100;  MinVersionEdit.BorderSpacing.Left := 10;  MinVersionEdit.TabOrder := 3;  MinVersionEdit.OnChange := @MinMaxVersionEditChange;   UseMaxVersionCheckBox := TCheckBox.Create(fOwner);  UseMaxVersionCheckBox.Parent := fOwner;  UseMaxVersionCheckBox.AnchorSideTop.Control := MaxVersionEdit;  UseMaxVersionCheckBox.AnchorSideTop.Side := asrCenter;  UseMaxVersionCheckBox.Left := 6;  UseMaxVersionCheckBox.Top := 43;  UseMaxVersionCheckBox.Width := 182;  UseMaxVersionCheckBox.TabOrder := 4;  UseMaxVersionCheckBox.Caption := lisPckEditMaximumVersion;  UseMaxVersionCheckBox.OnChange := @UseMaxVersionCheckBoxChange;   MaxVersionEdit := TEdit.Create(fOwner);  MaxVersionEdit.Parent := fOwner;  MaxVersionEdit.AnchorSideLeft.Control := UseMaxVersionCheckBox;  MaxVersionEdit.AnchorSideLeft.Side := asrBottom;  MaxVersionEdit.AnchorSideTop.Control := MinVersionEdit;  MaxVersionEdit.AnchorSideTop.Side := asrBottom;  MaxVersionEdit.Left := 204;  MaxVersionEdit.Top := 38;  MaxVersionEdit.Width := 100;  MaxVersionEdit.BorderSpacing.Left := 10;  MaxVersionEdit.BorderSpacing.Top := 2;  MaxVersionEdit.TabOrder := 5;  MaxVersionEdit.OnChange := @MinMaxVersionEditChange;   ApplyDependencyButton := TButton.Create(fOwner);  ApplyDependencyButton.Parent := fOwner;  ApplyDependencyButton.AnchorSideTop.Control := MaxVersionEdit;  ApplyDependencyButton.AnchorSideTop.Side := asrBottom;  ApplyDependencyButton.Left := 6;  ApplyDependencyButton.Top := 80;  ApplyDependencyButton.AutoSize := True;  ApplyDependencyButton.BorderSpacing.Top := 6;  ApplyDependencyButton.TabOrder := 6;  ApplyDependencyButton.Caption := lisPckEditApplyChanges; ---------------------------------------------------------------------- Blaazen · 2020-09-23 12:09 One note to Anchoring by code, see the attached screenshot. The equivalents are: the left button: AnchorParallel 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 Button1.AnchorParallel(akLeft, 0, Button2); the middle button: AnchorHorizontalCenterTo (or AnchorVerticalCenterTo) 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 Button1.AnchorHorizontalCenterTo(Button2); the right button: AnchorToNeighbour 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 Button1.AnchorToNeighbour(akLeft, 0, Button2); There is also method AnchorToCompanion, where 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   Button1.AnchorToCompanion(akLeft, 5, Button2);is combination of 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   Button1.AnchorToNeighbour(akLeft, 5, Button2);  Button1.AnchorParallel(akTop, 0, Button2);  Button1.AnchorParallel(akBottom, 0, Button2);