C#中怎樣在ToolStripMenuItem下再添加子級菜單

場景

在右鍵菜單ContextMenuStrip下添加子菜單選項能夠經過編程

ContextMenuStrip menuStrip
ToolStripMenuItem mnuChartOption = new ToolStripMenuItem();     //新建菜單項對象
mnuChartOption.Name = "chart_option";
mnuChartOption.Text = "圖形選項";
//點擊彈出圖形選項對話框
mnuChartOption.Click += delegate(object sender, EventArgs e)
   {
                
    };
menuStrip.Items.Add(mnuChartOption);

 

來實現。可是若是在右鍵菜單的子菜單選項下再添加子菜單選項,造成二級子菜單。spa

注: .net

博客主頁:
https://blog.csdn.net/badao_liumang_qizhi
關注公衆號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載 code

實現

ToolStripMenuItem 的子級菜單仍然是ToolStripMenuItem ,主要經過DropDownItems屬性來構建關聯關係。對象

構建右鍵二級子菜單示例代碼blog

     ToolStripMenuItem mnuDirectPrint = new ToolStripMenuItem("直接打印");
            mnuDirectPrint.Name = "directPrint";
            mnuDirectPrint.Text = "直接打印";
            mnuDirectPrint.Click += delegate(object sender, EventArgs e)
            {
                control.DoPrint();
            };

            ToolStripMenuItem mnuPrintPageSet = new ToolStripMenuItem("打印頁面設置");
            mnuPrintPageSet.Name = "printPageSet";
            mnuPrintPageSet.Text = "打印頁面設置";
            mnuPrintPageSet.Click += delegate(object sender, EventArgs e)
            {
                control.DoPageSetup();
            };

            ToolStripMenuItem mnuPrint = new ToolStripMenuItem();
            mnuPrint.Name = "mnuPrint";
            mnuPrint.Text = "打印";
            mnuPrint.DropDownItems.Add(mnuDirectPrint);
            mnuPrint.DropDownItems.Add(mnuPrintPageSet);

            
            menuStrip.Items.Add(mnuPrint);

效果

 

 

相關文章
相關標籤/搜索