Recent

Author Topic: Compile error when dynamically assigning menuItem.OnClick  (Read 3329 times)

stab

  • Full Member
  • ***
  • Posts: 237
Compile error when dynamically assigning menuItem.OnClick
« on: October 11, 2016, 09:40:09 pm »
My system:
Windows XP sp 3
Lazarus 1.6.0
FPC: 3.0.0

Can anyone help me out on this:
procedure TForm1.Button1Click(Sender: TObject);
var
  menuItem : TMenuItem;
begin
  menuItem := TMenuItem.Create(PopupMenu1);
  menuItem.Caption := 'Item added at ' + TimeToStr(now);
  menuItem.OnClick := PopupItemClick;

  //assign it a custom integer value..
  menuItem.Tag := GetTickCount;
  PopupMenu1.Items.Add(menuItem) ;
end;

procedure TForm1.PopupItemClick(Sender: TObject);
var
  menuItem : TMenuItem;
begin
  menuItem := TMenuItem(sender;
  ShowMessage(Format('Clicked on "%s", TAG value: %d',[menuItem.Name, menuItem.Tag]));
end;

When compiling, this statement: menuItem.OnClick := PopupItemClick gives
the following error:
unit1.pas(43,23) Error: Wrong number of parameters specified for call to "MenuItem1Click"

Think I've done according to cook-book, but what is wrong? %)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Compile error when dynamically assigning menuItem.OnClick
« Reply #1 on: October 11, 2016, 09:44:22 pm »
unit1.pas(43,23) Error: Wrong number of parameters specified for call to "MenuItem1Click"

Think I've done according to cook-book, but what is wrong? %)

Try:
Code: Pascal  [Select][+][-]
  1. menuItem.OnClick := @PopupItemClick;
  2.  
In this regards, Lazarus/FPC differs from Delphi. Without "@" FPC thinks you want to actually call/invoke the function.
« Last Edit: October 11, 2016, 09:46:45 pm by molly »

stab

  • Full Member
  • ***
  • Posts: 237
Re: Compile error when dynamically assigning menuItem.OnClick
« Reply #2 on: October 11, 2016, 09:46:41 pm »
Thanks alot, molly :D

ASerge

  • Hero Member
  • *****
  • Posts: 2475
Re: Compile error when dynamically assigning menuItem.OnClick
« Reply #3 on: October 12, 2016, 09:12:40 pm »
If you need compatibility with Delphi use this at the top of the file, without having to change code
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}
  2.   {$MODESWITCH CLASSICPROCVARS ON}
  3. {$ENDIF}

 

TinyPortal © 2005-2018