[Solved] Dynamic Popup won't work lazarus / 31-LCL ---------------------------------------------------------------------- rudi · 2018-02-25 09:10 Hello Together, I try to use a dynamic popup menu but the OnClick Event is not working. I use Lazarus 1.8.0 on Windows 10. I tried it on Windows 7 too, same problem. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1;{$mode objfpc}{$H+}interfaceuses        Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids, Types,Menus, StdCtrls; type TForm1 = class(TForm)                StringGrid1: TStringGrid;                procedure PopupReset(Sender: TObject);              procedure PopupUpDown(Sender : TObject);                procedure StringGrid1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);        private        public        end; var      Form1: TForm1; implementation {$R *.lfm} procedure TForm1.StringGrid1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);var  Popup : TPopupMenu;  mi,mb :       TMenuItem;begin  Popup := TPopupMenu.Create(StringGrid1);   mi := TMenuItem.Create(Popup);  mi.Caption := 'Reset';  mi.OnClick := @PopupReset;  Popup.Items.Add(mi);   mb := TMenuItem.Create(Popup);  mb.Caption := 'Change';  Popup.Items.Add(mb);   mi := TMenuItem.Create(mb);  mi.Caption := 'Up';  mi.OnClick := @PopupUpDown;  mi.Tag := 1;  mb.Add(mi);   mi := TMenuItem.Create(mb);  mi.Caption := 'Down';  mi.OnClick := @PopupUpDown;  mi.Tag := 2;  mb.Add(mi);   Popup.PopUp;         Popup.Free;end;  procedure TForm1.PopupReset(Sender : TObject);begin  StringGrid1.Cells[2,2] := 'Reset';end; procedure TForm1.PopupUpDown(Sender : TObject);begin        if TMenuItem(Sender).Tag=1 then                StringGrid1.Cells[2,2] := 'Up';        if TMenuItem(Sender).Tag=2 then                StringGrid1.Cells[2,2] := 'Down';end; end.  On the Mainform I placed a TStringGrid-Component and added the StringGridContextPopup-Action. The Project compiles without errors. The Popup menu opens and i can select the items incl. the submenu, but nothing happens. Any ideas? Thank you very much for your help. rudi ---------------------------------------------------------------------- taazz · 2018-02-25 09:31 Quote from: rudi on February 25, 2018, 10:10:33 amHello Together, I try to use a dynamic popup menu but the OnClick Event is not working. I use Lazarus 1.8.0 on Windows 10. I tried it on Windows 7 too, same problem. On the Mainform I placed a TStringGrid-Component and added the StringGridContextPopup-Action. The Project compiles without errors. The Popup menu opens and i can select the items incl. the submenu, but nothing happens. Any ideas? Thank you very much for your help. rudi Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.StringGrid1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);var  Popup : TPopupMenu;  mi,mb   :   TMenuItem;begin  Popup := TPopupMenu.Create(StringGrid1);   mi := TMenuItem.Create(Popup);  mi.Caption := 'Reset';  mi.OnClick := @PopupReset;  Popup.Items.Add(mi);   mb := TMenuItem.Create(Popup);  mb.Caption := 'Change';  Popup.Items.Add(mb);   mi := TMenuItem.Create(mb);  mi.Caption := 'Up';  mi.OnClick := @PopupUpDown;  mi.Tag := 1;  mb.Add(mi);   mi := TMenuItem.Create(mb);  mi.Caption := 'Down';  mi.OnClick := @PopupUpDown;  mi.Tag := 2;  mb.Add(mi);   Popup.PopUp; //<---- this does not pause the execution here.    Popup.Free;  //<----- this destroyes the dynamic popup before you have achance to select anything.end; I suggest to drop a popup menu on the form assign to the popup property of the grid and in menu's onpopup event clear the existing items and recreate them as needed. ---------------------------------------------------------------------- howardpc · 2018-02-25 12:28 I think you have uncovered a bug in the Windows implementation of TPopupMenu (when you select an item the widgetset fails to locate the correct TMenuItem in a dynamically created menu's Items list). The code you showed works as expected on linux without any alteration. taazz's suggestion is a workaround for Windows. ---------------------------------------------------------------------- rudi · 2018-02-25 14:24 Hi, I'm back from Brunch, thank you for your solutions. @taazz: I changed creating/freeing into FormCreate/FormDestroy. It works now as expected. My explanation: I tried to find out the order of execution: I find out that the popup menu works fine when i call 'popup.popup'. The Method 'ContextPopup' is waiting until Popup-Menu is finished. Then ContextPopup is finished (and Popup destroyed). When Popup menu would be 'alive' it now would executes OnClick-Command. Which is not possible because Menu is not existing after .free. @howardpc: I think in Linux OnClick-Action is performed directly from popup.popup. So it works in Linux. So i have learned: Take care of lifetime of objects/components. Thank you very much for your help. With best wishes, rudi ---------------------------------------------------------------------- ASerge · 2018-02-25 15:13 According to the documentation TrackPopupMenuEx: "A handle to the window that owns the shortcut menu. This window receives all messages from the menu. The window does not receive a WM_COMMAND message from the menu until the function returns." ---------------------------------------------------------------------- trayres · 2018-04-12 04:33 I just ran into this problem on Windows, Lazarus 1.8.2 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.PageControl1ContextPopup(Sender: TObject; MousePos: TPoint;  var Handled: boolean);var  tabindex: integer;  pt: TPoint;  APopupMenu: TPopupMenu;  APopupMenuItem: TMenuItem;begin  tabindex := TPageControl(Sender).IndexOfTabAt(MousePos);  if tabindex >= 0 then  begin    APopupMenu := TPopupMenu.Create(Self);    APopupMenuItem := TMenuItem.Create(APopupMenu); //FPageControlContextPopup    APopupMenuItem.Caption := 'Popup Menu test';    APopupMenuItem.OnClick := @PopupTest;    APopupMenu.Items.Add(APopupMenuItem);    APopupMenu.PopUp;    APopupMenu.Free; //<-Prevents OnClick from firing  end;end; ---------------------------------------------------------------------- balazsszekely · 2018-04-12 06:16 @trayres Declare APopupMenu as FPopupMenu in the Form private section. Use FormCreate and FormDestroy to create respectively free the object. You can re-use it each time is needed. ---------------------------------------------------------------------- jamie · 2018-04-12 21:49 In the above case I think doing a "Application.ProcessMessages" before the FREE will work.