Forum > LCL

toolbar example (my code isn't working)

(1/2) > >>

speter:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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. :o


--- Quote ---Error: Incompatible types: got "<procedure variable type of procedure(TObject) of object;Register>" expected "<procedure variable type of procedure(TObject);Register>"
--- End quote ---

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:
Type of the NewClick is procedure (sender: tobject) of object;
After changing the type of Tproc

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Tproc = procedure (sender: tobject) of object;error message is

--- Quote ---Error: Typed constants of the type "procedure of object" can only be initialized with NIL
--- End quote ---

speter:
Thanks very much @bytebites!

Here is where the code is at presently (it works):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version