Board index » delphi » Attach event to dynamically created menu item?

Attach event to dynamically created menu item?

Hi all,

I'm creating a Most Recently Used (MRU) list for my app. I've had
no problems creating the code to dynamically create the MRU items
in the File menu for my app, but where do I create the procedures
that get executed when the items are clicked, and how do I attach
the procedures to the dynamic menu items? TIA.

 - Dave
cyg...@mcs.com

 

Re:Attach event to dynamically created menu item?


Here is a snippet from my MRU code:
Just set the "OnClick" property to a method like this

    procedure MRUItemClick(Sender: TObject);
.
.
.

    NewItem := TMenuItem.Create(FileMenu);
    NewItem.OnClick := MRUItemClick;

Rick.

Quote
David M. Fornalsky wrote in message <6nr0cl$...@enews3.newsguy.com>...
>Hi all,

>I'm creating a Most Recently Used (MRU) list for my app. I've had
>no problems creating the code to dynamically create the MRU items
>in the File menu for my app, but where do I create the procedures
>that get executed when the items are clicked, and how do I attach
>the procedures to the dynamic menu items? TIA.

> - Dave
>cyg...@mcs.com

Re:Attach event to dynamically created menu item?


Hello David,

This may be in a FAQ. Its really easy, once you know how ! For
example in the Private section of your form, place

Procedure MRUClick(Sender : TObject);

This procedure you define within the unit, as

Procedure TMyForm.MRUClick(Sender : TObject);
...

When you add the Menu Item

MRU[bCount]:=TMenuItem.Create(Self);
MRU[bCount].Caption:=...;
MRU[bCount].OnClick:=MRUClick;

Hope that is sufficient to get you through

Ray Cramer
R...@Pol.ac.uk

Quote
David M. Fornalsky wrote:
> Hi all,

> I'm creating a Most Recently Used (MRU) list for my app. I've had
> no problems creating the code to dynamically create the MRU items
> in the File menu for my app, but where do I create the procedures
> that get executed when the items are clicked, and how do I attach
> the procedures to the dynamic menu items? TIA.

>  - Dave
> cyg...@mcs.com

Re:Attach event to dynamically created menu item?


Hi Ray (and everybody),

Thanks for the code. It helped and I've got it working. My only
question now would be, in the called procedure, how do I identify
which MRU item was selected?

 - Dave
cyg...@mcs.com

Quote
>>This may be in a FAQ. Its really easy, once you know how ! For
>>example in the Private section of your form, place

>>Procedure MRUClick(Sender : TObject);

>>This procedure you define within the unit, as

>>Procedure TMyForm.MRUClick(Sender : TObject);
>>...

>>When you add the Menu Item

>>MRU[bCount]:=TMenuItem.Create(Self);
>>MRU[bCount].Caption:=...;
>>MRU[bCount].OnClick:=MRUClick;

>>Hope that is sufficient to get you through

Other Threads