Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, menus; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} var mymenu: tmainmenu; item: tmenuitem; Procedure tform1.formcreate (Sender: tobject); begin mymenu: = tmainmenu. create (Self); self. menu: = mymenu; item: = tmenuitem. create (mymenu); item. caption: = 'a'; mymenu. items. add (item); item: = tmenuitem. create (mymenu); item. caption: = 'bb'; mymenu. items. add (item); item: = tmenuitem. create (mymenu); item. caption: = 'cc'; mymenu. items. add (item); {create a sub-menu based on the above} item: = tmenuitem. create (mymenu); item. caption: = 'a1'; mymenu. items [0]. add (item); {mymenu. items [0] points to the AA menu item, and now adds a subitem to AA} item: = tmenuitem. create (mymenu); item. caption: = 'a2 '; mymenu. items [0]. add (item); item: = tmenuitem. create (mymenu); item. caption: = 'a21'; mymenu. items [0] [1]. add (item); {mymenu. items [0] [1] points to the A2 menu item. Now add a subitem to A2} item: = tmenuitem. create (mymenu); item. caption: = 'a22'; mymenu. items [0] [1]. add (item); end.
:
Using another method to achieve the same effect seems more logical.
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, menus; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} var mymenu: tmainmenu; item: tmenuitem; Procedure tform1.formcreate (Sender: tobject); var itemd: tmenuitem; begin mymenu: = tmainmenu. create (Self); self. menu: = mymenu; item: = tmenuitem. create (mymenu); item. caption: = 'a'; mymenu. items. add (item); itemd: = tmenuitem. create (mymenu); {use itemd to create a new object} itemd. caption: = 'a1'; item. add (itemd); {the item points to the AA menu item at this time, and now adds a subitem to the AA} itemd: = tmenuitem. create (item); itemd. caption: = 'a2 '; item. add (itemd); itemd: = tmenuitem. create (item); itemd. caption: = 'a21'; item [1]. add (itemd); {item [1] at this time points to the A2 menu item, and now add a subitem to A2} itemd: = tmenuitem. create (item); itemd. caption: = 'a22'; item [1]. add (itemd); {Add sub-menu item to AA end} item: = tmenuitem. create (mymenu); item. caption: = 'bb'; mymenu. items. add (item); item: = tmenuitem. create (mymenu); item. caption: = 'cc'; mymenu. items. add (item); end.