Tmainmenu [3]-manually create a menu (2): Create a sub menu item

Source: Internet
Author: User
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.
 
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.