Link to home
Start Free TrialLog in
Avatar of Robert Laird
Robert LairdFlag for United States of America

asked on

Popup menu onclick doesn't fire event

I have a popup menu which dynamically creates captions based on the value in a grid (asg2), and it works like I want it to... except.... the OnClick event doesn't fire.

procedure TForm1.asg2MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  var ts :string;
      mi1,mi2 : TMenuItem;
begin
  if Button = mbRight then
    begin
      asg2.Row := ASG2.MouseCoord(X,Y).Y;
      AdvPopupMenu1.Items.Clear;

      mi1 := TMenuItem.Create(AdvPopupMenu1);
      mi1.Caption := 'Ping '+asg2.cells[0,asg2.row];
      mi1.OnClick := Ping2Click;

      mi2 := TMenuItem.Create(AdvPopupMenu1);
      mi2.Caption := 'Check '+asg2.cells[0,asg2.row];
      mi2.OnClick := Check1Click;
      AdvPopupMenu1.Items.Add([mi1,mi2]);

      if  ((ASG2.MouseCoord(X,Y).X=0)) then
          AdvPopupMenu1.PopupAtCursor;
      mi1.Destroy;
      mi2.Destroy;
    end;
end;

Open in new window


Neither Ping2Click nor Check1Click will fire when the respective menu item is selected.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of rfwoolf
rfwoolf
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Laird

ASKER

Since the menu is dynamic, I don't want it to be left around.  I just want it to pop-up when the user right-clicks on the grid cell -- which it does -- but when the user clicks on the menu it, either one, the OnClick event does not fire.  I.e., the Check1Click doesn't get called, and I'm trying to figure out why.  

Ah!  I commented out the destroy's, and now they are called.   Very interesting.

I tried it with m1.FreeOnRelease and that allows the events to be called, too.  Will that be enough to stop memory from growing?

His comments and question spurred me to remove the "destroy" command and that fixed the problem.