Adding buttons to TToolBar from runtime lazarus / 31-LCL ---------------------------------------------------------------------- CM630 · 2022-01-21 06:53 I am trying to add buttons to a TToolBar at runtime. In the code below (also attached), if I uncomment AddButtons(ToolBar, ['1', '2', '3', '4', '5']); in procedure TForm1.FormCreate(Sender: TObject); I get buttons ordered in the following way: 1  2  3  4  5 But if I uncomment only AddButtons(ToolBar, ['1', '2', '3', '4', '5']); in procedure TForm1.Button1Click(Sender: TObject); when I click Button 1 I get buttons ordered in the following way: 5  4  3  2  1 Am I doing something wrong or there is sth. wrong with Lazarus? 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, ComCtrls, StdCtrls;  type    { TForm1 }    TForm1 = class(TForm)    Button1: TButton;    ToolBar: TToolBar;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    public    end;  var  Form1: TForm1;  implementation  {$R *.lfm}  { TForm1 }  procedure AddButtons(ToolBar: TToolBar; const ButtonCaptions: array of String);var  i: integer;begin  for i := 0 to High(ButtonCaptions) do  begin    with TToolButton.Create(ToolBar) do    begin      Parent := ToolBar;      Caption := ButtonCaptions[i];      if (ButtonCaptions[i] = '|') then        Style := tbsSeparator      else        Style := tbsButton;      AutoSize := True;    end;  end;end;  procedure TForm1.FormCreate(Sender: TObject);begin  ToolBar.ShowCaptions := True;  //AddButtons(ToolBar, ['1', '2', '3', '4', '5']);end;  procedure TForm1.Button1Click(Sender: TObject);begin  //AddButtons(ToolBar, ['1', '2', '3', '4', '5']);end;  end. ---------------------------------------------------------------------- speter · 2022-01-21 10:44 I found the same result (Laz 2.2; Win 11). One quick fix is to add 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 left := parent.width;after line 62 (AutoSize := True;) in your code. procedure AddButtons() is from https://lazarus- ccr.sourceforge.io/docs/lcl/comctrls/ttoolbar.buttons.html so (if others find the same behaviour) maybe the example should be tweaked. cheers S. ---------------------------------------------------------------------- CM630 · 2022-01-21 12:29 Thanks, it works. Indeed, I have used the sample from https://lazarus- ccr.sourceforge.io/docs/lcl/comctrls/ttoolbar.buttons.html. I have added the example in the wiki: https://wiki.lazarus.freepascal.org/TToolBar#Example. ---------------------------------------------------------------------- wp · 2022-01-21 12:55 Quote from: speter on January 21, 2022, 11:44:05 amI found the same result (Laz 2.2; Win 11). One quick fix is to add 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 left := parent.width;after line 62 (AutoSize := True;) in your code. This does not help if the total width of all buttons is larger than the width of the toolbar; when the toolbar is filled the next buttons will be added in reverse order again. The fix also fails when the toolbar is vertical. I think it would be better to reverse the order in which the buttons are added: 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 AddButtons(ToolBar: TToolBar; const ButtonCaptions: array of String);var  i: integer;begin  for i := High(ButtonCaptions) downto 0 do    begin    with TToolButton.Create(ToolBar) do    begin      Parent := ToolBar;      Caption := ButtonCaptions[i];      if (ButtonCaptions[i] = '|') then        Style := tbsSeparator      else        Style := tbsButton;      AutoSize := True;    end;  end;end;  I am not sure, though, whether this behavious isn't a bug. IIRC there is a similar issue when a series of top-aligned controls are added to a parent - they always are in reverse order. Maybe this topic should be reported to the bug-tracker so that a developer can have a look. ---------------------------------------------------------------------- CM630 · 2022-01-21 13:10 Quote from: wp on January 21, 2022, 01:55:06 pm...I think it would be better to reverse the order in which the buttons are added:...I was also thinking about that, but as seen from my example, sometimes the buttons are added from left to right, and sometimes they are added from right to left. How is one to know what direction will be used, in order to select the proper adding order? Maybe if s.o. could try it on Delphi, it would be clear what the expected behaviour is.