RichTextBox 右键显示 ContextMenuTrip

说明:

(1)命名:

RichTextBox  -> rtxt1

ContextMenuTrip -> cms1

(2)截图:


(3)完整代码:

<span style="font-size:14px;">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Newemployee {
    public partial class Frm_ContextMenuTrip : Form {
        public Frm_ContextMenuTrip() {
            InitializeComponent();
        }
        
        //载入事件
        private void Frm_ContentMenuTrip_Load(object sender, EventArgs e) {
            //载入contextMenuTrip的子项
            ToolStripItem tsItem;
            tsItem = AddContextMenu("复制", cms1.Items, new EventHandler(Fuzhi_click));
            tsItem = AddContextMenu("-", cms1.Items, null);
            tsItem = AddContextMenu("粘贴", cms1.Items, new EventHandler(Naintie_click));
            tsItem = AddContextMenu("-", cms1.Items, null);
            tsItem = AddContextMenu("剪切", cms1.Items, new EventHandler(Jiantie_click));

            //右键显示的快捷菜单 contextMenuTrip
           <strong> <span style="color:#ff0000;">rtxt1.ContextMenuStrip = cms1;</span></strong>

        }

        /// <summary>
        /// 加入�子菜单
        /// </summary>
        /// <param name="text">要显示的文字,假设为 - 则显示为切割线</param>
        /// <param name="cms">要加入�到的子菜单集合</param>
        /// <param name="callback">点击时触发的事件</param>
        /// <returns>生成的子菜单,假设为分隔条则返回null</returns>
        ToolStripMenuItem <strong><span style="color:#ff0000;">AddContextMenu</span></strong>(string text, ToolStripItemCollection cms, EventHandler callback) {
            if (text == "-") {
                ToolStripSeparator tsp = new ToolStripSeparator();
                cms.Add(tsp);
                return null;
            } else if (!string.IsNullOrEmpty(text)) {
                ToolStripMenuItem tsmi = new ToolStripMenuItem(text);
                if (callback != null) {
                    tsmi.Click += callback;
                }
                cms.Add(tsmi);
                return tsmi;
            }
            return null;
        }

        //复制
        public  void Fuzhi_click(object sender, EventArgs e) {            
            rtxt1.Copy();
        }
        //粘贴
        public void Naintie_click(object sender, EventArgs e) {           
            rtxt1.Paste();
        }
        //剪贴
        public void Jiantie_click(object sender, EventArgs e) {          
            rtxt1.Cut();
        }
       
    }
}
</span>



 

posted @ 2014-11-07 17:46  hrhguanli  阅读(327)  评论(0编辑  收藏  举报