Problem in defining onClick in runtime with Pascal (Delphi / Lazarus)

1

I need to use procedure ClicaItem(Sender: TObject); in a OnClick created at run time by calling procedure CriaItem(nome:String); . However, in all my attempts, I could not assign item.OnClick to procedure ClicaItem . How do I proceed if an error occurs regarding incompatible types?

  

Error: Incompatible types: got address of   procedure (TObject); Register "expected" procedure variable type of   procedure (TObject) of object; Register > "

I want to clarify the procedure creation to the type accepted by OnClick, as well as how to do this assignment in the OnClick event.

NOTE: The item is normally created in runtime, but the line referring to OnClick is commented on due to the error before compiling.

    
asked by anonymous 27.03.2016 / 08:03

1 answer

2

Your ClicaItem procedure must belong to an instance of a class (object);

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
     procedure ClicaItem(Sender: TObject);
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
   Button1.Click := ClicaItem;
end;

procedure TForm1.ClicaItem(Sender: TObject);
begin
   ShowMessage('ClicaItem');
end;

end.
    
28.03.2016 / 13:32