1. 程式人生 > >建立級聯選單

建立級聯選單

實現效果:

  

知識運用:

  DropDownItems選單項集合的Add方法   //將指定的選單項新增到選單項集合

  public int Add(ToolStripItem value)

實現程式碼:

        private void Form1_Load(object sender, EventArgs e)
        {
            ToolStripMenuItem P_ts = (ToolStripMenuItem)menuStrip1.Items[0];    //得到檔案選單項
            ToolStripMenuItem ts1 = new ToolStripMenuItem("開啟文字檔案");
            ToolStripMenuItem ts2 = new ToolStripMenuItem("開啟XML檔案");       //建立選單項
            ToolStripMenuItem ts3 = new ToolStripMenuItem("開啟JPG檔案");
            ToolStripMenuItem ts4 = new ToolStripMenuItem("開啟BMP檔案");
            ToolStripMenuItem P_ts2 = (ToolStripMenuItem)P_ts.DropDownItems[0]; //得到子選單項
            P_ts2.DropDownItems.Add(ts1);
            P_ts2.DropDownItems.Add(ts2);                                       //新增選單項
            P_ts2.DropDownItems.Add(ts3);
            P_ts2.DropDownItems.Add(ts4);
        }