Recent

Author Topic: How to assign event procedure at run-time  (Read 7257 times)

dkjMusic

  • Full Member
  • ***
  • Posts: 146
How to assign event procedure at run-time
« on: December 25, 2010, 03:04:49 pm »
From all I can find the following should work:

procedure TForm1.CreateItemButtons();
var
   i: Integer;
begin
     for i := 1 to 12 do begin
         ItemButton := TSpeedButton.Create(Application);
         with ItemButton do begin
              Parent := ToolBar1;
              Caption := IntToStr(i);  Top := 2; Height := 22; Width := 30;
              Left := 173+(i+1)*Width;
              GroupIndex := 1;
              OnClick := @ToolbarItemsHandler;
         end;
     end;
end;

procedure TForm1.ToolbarItemsHandler();
var
   i, Item: Integer;
begin
     for i := 1 to 12 do begin
         if ItemButton.Down = True then begin
            Item := i;
            ShowMessage('Item '+IntToStr(Item)+' selected.');
         end;
     end;
end;

However, I get the following compile error:
main.pas(133,26) Error: Incompatible types: got "<procedure variable type of procedure of object;Register>" expected "<procedure variable type of procedure(TObject) of object;Register>"

What am I doing wrong?
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to assign event procedure at run-time
« Reply #1 on: December 25, 2010, 03:10:12 pm »
Parameters must be the same, try this:

Code: [Select]
procedure TForm1.ToolbarItemsHandler(Sender: TObject);

dkjMusic

  • Full Member
  • ***
  • Posts: 146
Re: How to assign event procedure at run-time
« Reply #2 on: December 25, 2010, 03:28:25 pm »
Parameters must be the same, try this:

Code: [Select]
procedure TForm1.ToolbarItemsHandler(Sender: TObject);

Yep, that did the trick. Thanks so much and Happy Holiday.  :)
Windows 7 Ultimate/32 bit;
Lazarus 0.9.31/Rev 33300;
FPC 2.7.1/Rev 19464;
GDB 7.0.50;
(via CodeTyphon 2.30)

 

TinyPortal © 2005-2018