Recent

Author Topic: Popup Menu cannot set on onClick event  (Read 500 times)

Raf20076

  • Full Member
  • ***
  • Posts: 173
    • https://github.com/Raf20076
Popup Menu cannot set on onClick event
« on: January 21, 2021, 10:58:41 am »
Hi Guys

I cannot go through with this. I want to create kind of popup menu dinamiacally with onClick event but I get errors.

Compile Project, Target: C:\Users\Dom\AppData\Local\Temp\project1.exe: Exit code 1, Errors: 2
unit1.pas(42,33) Error: Wrong number of parameters specified for call to "PopupMenuItemsClick"
unit1.pas(19,15) Error: Found declaration: PopupMenuItemsClick(TObject);



Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Menus;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Memo1: TMemo;
  16.     Memo2: TMemo;
  17.     PopupMenu1: TPopupMenu;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure PopupMenuItemsClick(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. var
  35. NewItem : TMenuItem;
  36. Popupword :String ;
  37. begin
  38.              Popupword := 'ddddddd';
  39.              NewItem := TMenuItem.Create(PopupMenu1);
  40.              PopupMenu1.Items.Add(NewItem);
  41.              NewItem.Caption := Popupword;
  42.              NewItem.OnClick := PopupMenuItemsClick; // Assign it an event handler.
  43.  
  44.          end;
  45.  
  46.  
  47. procedure TForm1.PopupMenuItemsClick(Sender: TObject);
  48. begin
  49.  
  50.       ShowMessage('item clicked');
  51.  
  52.      end;
  53. end.
  54.  
  55.  
  56.  

Thanks

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Popup Menu cannot set on onClick event
« Reply #1 on: January 21, 2021, 11:05:48 am »
In mode objfpc you must use the @ operator to assign a procedure to the event handler:
Code: Pascal  [Select][+][-]
  1. NewItem.OnClick := @PopupMenuItemsClick;

Alternatively you can use mode Delphi instead of objfpc
Code: Pascal  [Select][+][-]
  1. //{$mode objfpc}{$H+}
  2. {$mode Delphi}

Raf20076

  • Full Member
  • ***
  • Posts: 173
    • https://github.com/Raf20076
Re: Popup Menu cannot set on onClick event
« Reply #2 on: January 21, 2021, 11:14:56 am »
In mode objfpc you must use the @ operator to assign a procedure to the event handler:
Code: Pascal  [Select][+][-]
  1. NewItem.OnClick := @PopupMenuItemsClick;

Alternatively you can use mode Delphi instead of objfpc
Code: Pascal  [Select][+][-]
  1. //{$mode objfpc}{$H+}
  2. {$mode Delphi}


Thanks a lot it works.


 

TinyPortal © 2005-2018