Recent

Author Topic: [SOLVED] PopupMenu Creation OnClick Error  (Read 4748 times)

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
[SOLVED] PopupMenu Creation OnClick Error
« on: July 21, 2017, 03:44:39 am »
I am designing a debug unit and trying to create a popup menu for use to access debug information.

unit tstDebugPop1;

{.MODE Delphi}
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Menus;

var
  fPopMenu1 :TPopupMenu;
  gDebug    :TStringList;   


procedure CreatePopup;
var vItem:TMenuItem;
begin
  fPopMenu1:=TPopupMenu.Create(nil);
  vItem:=TMenuItem.Create(fPopMenu1);
  vItem.Caption:='&Show last';
  vItem.OnClick:=@DebugShowLast;
  fPopMenu1.Items.Add(vItem);
end;

CreatePopup is executed within the Initialization section of the unit.

An error occurs for the line:
  vItem.OnClick:=@DebugShowLast;
and shows the following error:
tstdebugpop1.pas(148,18) Error: Incompatible types: got "Pointer" expected "<procedure variable type of procedure(TObject) of object;Register>"

DebugShowLast is a procedure within the unit.

If I comment out that line, the program runs and the popup menu is created. I link the popup to the main form, and a right click produces the menu with the sole item "Show last". So the creation of the menu and item is working ok, but the link is not.

I have tried the Delphi modes and the {$mode objfpc}{$H+} mode above.

So what is wrong with:
  vItem.OnClick:=@DebugShowLast;   >:(

TIA

« Last Edit: September 09, 2017, 06:51:52 am by Bazzao »
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: PopupMenu Creation OnClick Error
« Reply #1 on: July 21, 2017, 04:01:29 am »
OnClick has to be a method, not common standalone procedure.

Read more here:
http://forum.lazarus.freepascal.org/index.php/topic,36677.msg244671.html#msg244671

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: PopupMenu Creation OnClick Error
« Reply #2 on: July 21, 2017, 05:09:22 am »
Thanks for that. Lots of little snippets of coding, hard to work out. But I progressed ...

No compile errors, but nil result.

I now have

Type
  TBtnClick = TNotifyEvent;

var
  BtnClick  :TBtnClick;

procedure CreatePopup;
var vItem:TMenuItem;
begin
  fPopMenu1:=TPopupMenu.Create(nil);
  vItem:=TMenuItem.Create(fPopMenu1);
  vItem.Caption:='&Show last';
  vItem.OnClick:=BtnClick;
  fPopMenu1.Items.Add(vItem);
end;

But I still don't know how to call DebugShowLast.



Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: PopupMenu Creation OnClick Error
« Reply #3 on: July 21, 2017, 06:05:17 am »
It is simplest to make the form the container for everything, since the debug procedure has to be a procedure of object (a form method).
Something like this:

Code: Pascal  [Select][+][-]
  1. ...
  2. uses
  3.   SysUtils, Forms, Dialogs, Menus;
  4.  
  5. type
  6.  
  7.   TFormToDebug = class(TForm)
  8.     procedure FormCreate(Sender: TObject);
  9.   private
  10.     FDebugPopup: TPopupMenu;
  11.     function NewDebugPopupMenu: TPopupMenu;
  12.     procedure DebugShowLast(Sender: TObject);
  13.   public
  14.  
  15.   end;
  16.  
  17. var
  18.   FormToDebug: TFormToDebug;
  19.  
  20. implementation
  21.  
  22. procedure TFormToDebug.DebugShowLast(Sender: TObject);
  23. begin
  24.   ShowMessage('DebugShowLast');
  25.   // your code here
  26. end;
  27.  
  28. procedure TFormToDebug.FormCreate(Sender: TObject);
  29. begin
  30.   PopupMenu:=NewDebugPopupMenu;
  31.   PopupMode:=pmAuto;
  32. end;
  33.  
  34. function TFormToDebug.NewDebugPopupMenu: TPopupMenu;
  35. var
  36.   vItem: TMenuItem;
  37. begin
  38.   Result:=TPopupMenu.Create(Self);
  39.   vItem:=TMenuItem.Create(Result);
  40.   vItem.Caption:='&Show last';
  41.   vItem.OnClick:=@DebugShowLast;
  42.   Result.Items.Add(vItem);
  43. end;
  44. ...

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: PopupMenu Creation OnClick Error
« Reply #4 on: July 21, 2017, 06:13:03 am »
Oops, I'm late. Here is my test.zip:

Code: Pascal  [Select][+][-]
  1. unit mydebugunit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Menus, Controls, Dialogs;
  9.  
  10. type
  11.  
  12.   { TmyPopupMenu }
  13.  
  14.   TmyPopupMenu = class(TPopupMenu)
  15.     procedure DebugShowLast(Sender: TObject);
  16.   end;
  17.  
  18. var
  19.   fPopMenu1: TmyPopupMenu;
  20.   gDebug   : TStringList;
  21.  
  22. procedure CreatePopup;
  23. procedure ShowPopup;
  24. procedure FreePopup;
  25.  
  26. implementation
  27.  
  28. procedure CreatePopup;
  29. var vItem:TMenuItem;
  30. begin
  31.   fPopMenu1:=TmyPopupMenu.Create(nil);
  32.   vItem:=TMenuItem.Create(fPopMenu1);
  33.   vItem.Caption:='&Show last';
  34.   vItem.OnClick:=@fPopMenu1.DebugShowLast;
  35.   fPopMenu1.Items.Add(vItem);
  36. end;
  37.  
  38. procedure ShowPopup;
  39. begin
  40.   fPopMenu1.PopUp(Mouse.CursorPos.x, Mouse.CursorPos.y);
  41. end;
  42.  
  43. procedure FreePopup;
  44. begin
  45.   fPopMenu1.Free;
  46. end;
  47.  
  48. { TmyPopupMenu }
  49.  
  50. procedure TmyPopupMenu.DebugShowLast(Sender: TObject);
  51. begin
  52.   ShowMessage('<< say something here >>');
  53. end;
  54.  
  55. end.

edit: I forgot to attach the file.
« Last Edit: July 21, 2017, 06:15:52 am by Handoko »

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: PopupMenu Creation OnClick Error
« Reply #5 on: July 21, 2017, 06:16:36 am »
Yes, that would do. Except that Debug is a standalone unit to be used in various projects. The object of the exercise is to "Use Debug" until no longer needed and then turn off compiler directives so it no longer is part of the compilation. Automated create and link processes should remain with that unit, with only the assigning of the Popup to the form and assigning the parent of the Popup the 2 simple commands within the main form. Those last 2 are done.

So the missing link is getting a standalone procedure within the Debug unit to be called via the code already supplied.

B


Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: PopupMenu Creation OnClick Error
« Reply #6 on: July 21, 2017, 06:18:31 am »
Thanks for the update Handoko. Looking at your files now ...
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: PopupMenu Creation OnClick Error
« Reply #7 on: July 21, 2017, 06:28:32 am »
Thanks Handoko. That works. I just picked the necessary bits out and, lo and behold, I get the fully functioning popup menu (one item).

Now for the other items, but that should be no problem.

Many thanks.
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: PopupMenu Creation OnClick Error
« Reply #8 on: July 21, 2017, 06:54:16 am »
Glad to hear it works.


Actually, howardpc's version is better. That will be easier to manage when your code grow big. For some rare cases, I choose my version because I prefer the 'procedural' way.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: PopupMenu Creation OnClick Error
« Reply #9 on: July 21, 2017, 11:27:59 am »
Glad to hear it works. Actually, howardpc's version is better. That will be easier to manage when your code grow big.

howardpc's way probably for internal debug routines.

The object of my exercise is to create a unit that can be used by any project with calls to routines to add data to a stringlist, reset or clear the data. The highlighted Showlast procedure brings up the last 30 stringlist entries in a ShowMessage box. And a copy to clipboard popup menu item dumps the entire stringlist onto the clipboard.

The main application just calls an add to the debug stringlist when debugging is needed.

The whole unit is designed to be written out by commenting out a conditional define.

BTW, One of the Popup items was a check toggle. After creation of the item, I saved the index number in a private global variable and have a global boolean value the main application can use. This allows a Verbose setting where more data is dumped if Verbose is true.

But I'll keep howards on file anyway.

Thanks again to Handoko and howardpc for their input.

B

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

 

TinyPortal © 2005-2018