unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, menus; Type tform1 = Class (tform) procedure formcreate (Sender: tobject); Procedure proc (Sender: tobject); {declare the method to be called} end; var form1: tform1; implementation {$ R *. DFM} var mymenu: tmainmenu; item: tmenuitem; {method to be called} procedure tform1.proc (Sender: tobject); begin If sender is tmenuitem then showmessage (tmenuitem (sender ). caption); end; Procedure tform1.formcreate (Sender: tobject); var itemd: tmenuitem; begin mymenu: = tmainmenu. create (Self); mymenu. autohotkeys: = mamanual; self. menu: = mymenu; item: = tmenuitem. create (mymenu); item. caption: = 'a'; mymenu. items. add (item); itemd: = tmenuitem. create (mymenu); itemd. caption: = 'a & 1'; itemd. onclick: = proc; {call method} item. add (itemd); itemd: = tmenuitem. create (item); itemd. caption: = 'a & 2'; itemd. onclick: = proc; {call method} item. add (itemd); end.
:
However, menu events generally use actionlist. You can add an actionlist and a method to implement the same functions as above:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, menus, actnlist; Type tform1 = Class (tform) actionlist1: tactionlist; Action1: taction; Procedure formcreate (Sender: tobject); Procedure action1execute (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} var mymenu: tmainmenu; item: tmenuitem; Procedure tform1.action1execute (Sender: tobject); begin If sender is tmenuitem then showmessage (tmenuitem (sender ). caption); end; Procedure tform1.formcreate (Sender: tobject); var itemd: tmenuitem; begin mymenu: = tmainmenu. create (Self); mymenu. autohotkeys: = mamanual; self. menu: = mymenu; item: = tmenuitem. create (mymenu); item. caption: = 'a'; mymenu. items. add (item); itemd: = tmenuitem. create (mymenu); itemd. caption: = 'a & 1'; itemd. onclick: = action1execute; {call method} item. add (itemd); itemd: = tmenuitem. create (item); itemd. caption: = 'a & 2'; itemd. onclick: = action1execute; {call method} item. add (itemd); end.