Board index » delphi » In a dynamic popup menu how do you response to on click?

In a dynamic popup menu how do you response to on click?

Hi,

I'm using Delphi 1 (it was free :-)) and I am trying to make a win95
system tray app where when I click the tray icon a menu pops up.  I'm
using a freeware app called tray icon to get the tray icon.  This has
an on click event that I'm using to popup a popupmenu component.  It's
shame that has got an align bottom right ability, I have to fiddle the
X,Y using the numbers from trayicon and a offset based on the number
of menu lines.

Anyway the problem is that the popup is dynamic i.e. designed at
run-time.  I've used this

  NewItem := TMenuItem.Create(PopupMenu);
  NewItem.Caption := 'Another One';
  PopupMenu.Items.Add(NewItem);

to add items to the popup list.  But how to I program an on click
event to determine what was selected?  Popupmenu doesn't have a on
click event the menuitem does but that's no decided until run time.

Anyone got a better solution?  I'd like to keep the ticked option that
you get with popupmenu.

TIA

...malcolm

--  Malcolm Reeves  MIRSE AMIEE (mree...@dial.pipex.com) Chippenham, UK
--  Full Circuit Ltd - Design Service for Analogue/Digital H/W & S/W
--  Railway Signalling and Power electronics. For more details and
--  Win95 DUN & Pspice tips, see:

--  http://dspace.dial.pipex.com/mreeves/

-- PS Spam merchants - I ALWAYS COMPLAIN TO YOUR ISP!

 

Re:In a dynamic popup menu how do you response to on click?


Quote
>  NewItem := TMenuItem.Create(PopupMenu);
>  NewItem.Caption := 'Another One';
>  PopupMenu.Items.Add(NewItem);

Malcolm, this is how I implement this;

PopupMenu.Items.Add(TMenuItem.Create(Self));
 PopupMenu.Items[PopupMenu.Items.Count-1].Name:='AboutMenuItem';
 PopupMenu.Items[PopupMenu.Items.Count-1].Caption:='&About ...';
 PopupMenu.Items[PopupMenu.Items.Count-1].OnClick:=AboutClick;
...
procedure TForm1.AboutClick(Sender: TObject);
begin
    // process click here
end;

However, your implementation will work as well written like this;

NewItem := TMenuItem.Create(PopupMenu);
NewItem.Caption := 'Another One';
NewItem.OnClick:=MenuItemClick;
PopupMenu.Items.Add(NewItem);
...
procedure TForm1.MenuItemClick(Sender: TObject);
begin
    // process here
end;

Hope this helps.

Chris Marsh
chri...@lineone.net

Re:In a dynamic popup menu how do you response to on click?


Hi,

Thanks to those who replied and sorted out my problem :-).  Especially
Alan Lloyd who emailed me a working program he knocked just for me by
the look of it.

...malcolm

--  Malcolm Reeves  MIRSE AMIEE (mree...@dial.pipex.com) Chippenham, UK
--  Full Circuit Ltd - Design Service for Analogue/Digital H/W & S/W
--  Railway Signalling and Power electronics. For more details and
--  Win95 DUN & Pspice tips, see:

--  http://dspace.dial.pipex.com/mreeves/

-- PS Spam merchants - I ALWAYS COMPLAIN TO YOUR ISP!

Re:In a dynamic popup menu how do you response to on click?


Quote
> Anyway the problem is that the popup is dynamic i.e. designed at
> run-time.  I've used this

>   NewItem := TMenuItem.Create(PopupMenu);
>   NewItem.Caption := 'Another One';
>   PopupMenu.Items.Add(NewItem);

> to add items to the popup list.  But how to I program an on click

NewItem.OnClick := @same_proc;

proc same_proc

if Sender is TMenuItem then
  with Sender as TMenuItem do begin
    if Caption = <the_text> then
  end;

Quote
> event to determine what was selected?  Popupmenu doesn't have a on
> click event the menuitem does but that's no decided until run time.

You can add more code in same_proc

Re:In a dynamic popup menu how do you response to on click?


Quote
>    if Caption = <the_text> then

It would be far easier to specify the 'tag' property of the TMenuItem to
identify the item selected.

Chris Marsh

Other Threads