toolbar example (my code isn't working) lazarus / 31-LCL ---------------------------------------------------------------------- speter · 2022-01-23 01:18 G'Day Folks, In the thread "Adding buttons to TToolBar from runtime" (https://forum.lazarus.freepascal.org/index.php/topic,57957.0.html) there was a Ttoolbar example (https://lazarus- ccr.sourceforge.io/docs/lcl/comctrls/ttoolbar.buttons.html). In looking at the example I thought that it would be more interesting / useful if it included an onClick event handler. I wrote some code to attempt to implement this but an getting an error. In part, my code is: 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 type  Tproc = procedure (sender: tobject);  TMyRec = record    cap : string;    clk : Tproc;  end;  TMyArray = array of TMyRec;   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Memo1: TMemo;    Panel1: TPanel;    ToolBar1: TToolBar;    procedure Button1Click(Sender: TObject);  private   public    procedure NewClick(Sender: TObject);  procedure SaveClick(Sender: TObject);    procedure CutClick(Sender: TObject);    procedure CopyClick(Sender: TObject);    procedure PasteClick(Sender: TObject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  tools : TMyArray = ( (cap:'New'; clk:@NewClick),                       (cap:'Save';clk:@SaveClick) );//                                     ^^^^^^^^ does NOT work I am getting an error message referring to lines 38 & 39 above. The emphasis "of object" is mine. QuoteError: Incompatible types: got "" expected "" So, I think (maybe) my code is close to correct, but clearly isn't quite right! I am attaching the lazarus project to this message. Any suggestions would be welcomed. cheers S. ---------------------------------------------------------------------- bytebites · 2022-01-23 09:10 Type of the NewClick is procedure (sender: tobject) of object; After changing the type of Tproc 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 Tproc = procedure (sender: tobject) of object;error message is QuoteError: Typed constants of the type "procedure of object" can only be initialized with NIL ---------------------------------------------------------------------- speter · 2022-01-23 10:48 Thanks very much @bytebites! Here is where the code is at presently (it works): 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 type  Tproc = procedure (sender: tobject) of object; ...procedure TForm1.Button1Click(Sender: TObject);   procedure FillToolbar (ToolBar: TToolBar;                         const caps : array of string;                         const events : array of Tproc);  // based on https://lazarus- ccr.sourceforge.io/docs/lcl/comctrls/ttoolbar.buttons.html  var    i: integer;  begin    for i := 0 to High(caps) do    begin      with TToolButton.Create(ToolBar) do      begin        Parent := ToolBar;      Caption := caps[i];        if (caps[i] = '|') then          Style := tbsSeparator        else          begin            Style := tbsButton;            onclick := events[i];          end;        AutoSize := True;        left := parent.width;      end;    end;  end; begin  FillToolbar(ToolBar1, ['New', 'Save', '|', 'Cut', 'Copy', 'Paste'],              [@NewClick,@SaveClick,nil,@CutClick,@CopyClick,@PasteClick] );end; ... This is OK; but I'd like to use (something like) the record in my first post. cheers S. ---------------------------------------------------------------------- wp · 2022-01-23 10:56 There's no need to declare a new type TProc for "procedure (Sender: TObject) of object" since this is a TNotifyEvent used all over the LCL. ---------------------------------------------------------------------- speter · 2022-01-23 23:24 Thanks WP and Jamie. Jamie: could you post a simple project (using Add/Insert) that implements the idea shown in my second post!? I couldn't see out how to add an event handler. cheers S. ---------------------------------------------------------------------- jamie · 2022-01-23 23:48 My point is "It should be that way but isn't" The buttons array exists but only serves as a reading property and inserting new buttons does not seem to assign it to Toolbar as parent so it's a bust! ---------------------------------------------------------------------- jamie · 2022-01-24 00:19 I think maybe you should be using a single event. Point them all to the same event and when in that event you can identify them by checking the TAG variable for example which could be the numerical position of where they live in the array.  When you add the button to the tool bar set the TAG property of each button to that of the array position so you can identify the correct button while on the TNotifiyEvent handler.  I can maybe bang out some code to show you if needed. ---------------------------------------------------------------------- jamie · 2022-01-24 00:48 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, ExtCtrls, ComCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    ToolBar1: TToolBar;    procedure Button1Click(Sender: TObject);  private   public    procedure DoBUttons(Sender:Tobject);  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }Procedure TForm1.DoButtons(Sender:TObject);Begin   Case TtoolButton(Sender).Tag Of    0:Caption := 'Copy'; //{Do Copy};    1:Caption := 'Paste';//{Do Paste};   end;end; Procedure AddMeToButtons(AToolbar:TToolBar; AClickEvent:TNotifyEvent; AButtons:Array of String);Var  I :Integer;Begin     for i := 0 to High(AButtons) do    begin      with TToolButton.Create(AToolBar) do      begin        Parent := AToolBar;        Caption := AButtons[i];        if (Abuttons[i] = '|') then        Style := tbsSeparator        else          begin            Style := tbsButton;            onclick := AClickEvent;          end;        AutoSize := True;        Visible := true;        left := parent.width;        Tag := I;// Array Index;      end;    end;end; procedure TForm1.Button1Click(Sender: TObject);begin   AddMeToButtons(ToolBar1, @doButtons,['Copy','Paste']);end; end.    have a look at that. ---------------------------------------------------------------------- CM630 · 2022-01-26 08:19 As @Jamie pointed out - you should better use the tag property. And the reason is that looking in your code: 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   FillToolbar(ToolBar1, ['New', 'Save', '|', 'Cut', 'Copy', 'Paste'],              [@NewClick,@SaveClick,nil,@CutClick,@CopyClick,@PasteClick] );You are creating the buttons during runtime, but you have created the procedures NewClick, SaveClick before that (during design time). Then maybe there is not much reason for not creating the buttons during design time, too. For example, if you want to make a calculator, you will just    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 AddMeToButtons(ToolBar1, @doButtons,['Copy','Paste']); and then 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.DoButtons(Sender:TObject); ...   Text1.Text := Text1.Text + ToolBar.Butons[TtoolButton(Sender).Tag].Caption;...end; ---------------------------------------------------------------------- speter · 2022-01-26 22:56 Thanks for that. I am not sure why the wiki example adds buttons at runtime; of hand, I can't think of any good use for this. My intention was to try to improve the example, but I don't think it is worth worrying about. cheers S.