C# 創建彈出式菜單(ToolStripMenuItem)

來源:互聯網
上載者:User
方法:
    1、創建一個菜單項數組。
    2、在數組中增加菜單項
    3、創建一個彈出式引用菜單
    4、將菜單與菜單項加入到彈出式引用菜單中
    5、將控制項的ContextMenuStrip屬性設置成彈出菜單

如下例:
  
例1:            //ToolStripMenuItem:表示顯示在 MenuStrip 或 ContextMenuStrip 上的可選取選項。
            //1、定義菜單項目集
            ToolStripMenuItem[] formMenuItemList = new ToolStripMenuItem[2];
            //2、增加菜單項目集內容及對應方法
            formMenuItemList[0] = new ToolStripMenuItem("Save Member",null,new EventHandler(saveMemberClick));
            formMenuItemList[1] = new ToolStripMenuItem("Clear",null,new EventHandler(clearClick));
            //ContextMenuStrip:表示快速鍵功能表。
            //3、定義快速功能表
            ContextMenuStrip formMenu = new ContextMenuStrip();
            //4、將菜單加入到功能表中
            formMenu.Items.AddRange(formMenuItemList);
            //5、調用快速功能表
            this.ContextMenuStrip = formMenu;

  
例2:            //1、ToolStripMenuItem:表示顯示在 MenuStrip 或 ContextMenuStrip 上的可選取選項。
            ToolStripItem[] tsmRtbMenuu = new ToolStripItem[6];

            //2、增加菜單項目集內容及對應方法
            tsmRtbMenuu[0] = new ToolStripMenuItem("儲存GUID", null, new EventHandler(btnWrite_Click));
            tsmRtbMenuu[1] = new ToolStripMenuItem("另存GUID", null, new EventHandler(aonterSaveToolStripMenuItem_Click));
            //增加菜單項目分隔線

            tsmRtbMenuu[2] = new ToolStripSeparator();
            tsmRtbMenuu[3] = new ToolStripMenuItem("複製", null, new EventHandler(rtbCopy_changed));
            tsmRtbMenuu[4] = new ToolStripSeparator();
            tsmRtbMenuu[5] = new ToolStripMenuItem("列印", null, new EventHandler(printToolStripMenuItem_Click));

            //3、定義快速功能表
            ContextMenuStrip ctmsRtbMenu = new ContextMenuStrip();

            //4、將菜單加入到功能表中
            ctmsRtbMenu.Items.AddRange(tsmRtbMenuu);

            //5、調用快速功能表(將功能表加入到控制項右鍵菜單中)
            //rtbGuidValue為RichTextBox控制項
            rtbGuidValue.ContextMenuStrip = ctmsRtbMenu;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.