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

本文介绍如何使用C#创建弹出式右键菜单,并通过两个示例详细展示了定义菜单项、添加事件处理程序以及将菜单关联到控件的过程。
方法:
    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 "nullnew EventHandler(btnWrite_Click));
            tsmRtbMenuu[ 1] =  new ToolStripMenuItem( " 另存GUID "nullnew EventHandler(aonterSaveToolStripMenuItem_Click));
             // 增加菜單項目分隔線
            tsmRtbMenuu[ 2] =  new ToolStripSeparator();
            tsmRtbMenuu[ 3] =  new ToolStripMenuItem( " 複製 "nullnew EventHandler(rtbCopy_changed));
            tsmRtbMenuu[ 4] =  new ToolStripSeparator();
            tsmRtbMenuu[ 5] =  new ToolStripMenuItem( " 打印 "nullnew EventHandler(printToolStripMenuItem_Click));
             //3、 定義快速功能表
            ContextMenuStrip ctmsRtbMenu =  new ContextMenuStrip();
             //4、 將菜單加入到功能表中
            ctmsRtbMenu.Items.AddRange(tsmRtbMenuu);
             //5、 調用快速功能表(將功能表加入到控件右鍵菜單中)
             // rtbGuidValue為RichTextBox控件
            rtbGuidValue.ContextMenuStrip = ctmsRtbMenu;


转自:http://www.cnblogs.com/scottckt/archive/2007/11/21/967719.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值