为RichTextBox控件添加上下文菜单

private void Form1_Load(object sender, EventArgs e)
        {//为RichTextBox控件添加上下文菜单
            ContextMenuStrip MenuStrip1 = new System.Windows.Forms.ContextMenuStrip();
            MenuStrip1.Name = "menuStrip1";
            MenuStrip1.Text = "menuStrip1";
            ToolStripMenuItem MyItem1 = new ToolStripMenuItem();
            MyItem1.Text = "全选(&A)";
            ToolStripMenuItem MyItem2 = new ToolStripMenuItem();
            MyItem2.Text = "剪切(&X)";
            ToolStripMenuItem MyItem3 = new ToolStripMenuItem();
            MyItem3.Text = "复制(&C)";
            ToolStripMenuItem MyItem4 = new ToolStripMenuItem();
            MyItem4.Text = "粘贴(&P)";            
            MenuStrip1.Items.Add(MyItem1);
            MenuStrip1.Items.Add(MyItem2);
            MenuStrip1.Items.Add(MyItem3);
            MenuStrip1.Items.Add(MyItem4);
            this.richTextBox1.ContextMenuStrip = MenuStrip1;
            MyItem1.Click += new System.EventHandler(this.MyItem1_Click);
            MyItem2.Click += new System.EventHandler(this.MyItem2_Click);
            MyItem3.Click += new System.EventHandler(this.MyItem3_Click);
            MyItem4.Click += new System.EventHandler(this.MyItem4_Click); 
        }
        private void MyItem1_Click(object sender, EventArgs e)
        {//全选菜单
            this.richTextBox1.SelectAll();
        }
        private void MyItem2_Click(object sender, EventArgs e)
        {//剪切菜单
            this.richTextBox1.Cut();
        }
        private void MyItem3_Click(object sender, EventArgs e)
        {//复制菜单
            this.richTextBox1.Copy();
        }
        private void MyItem4_Click(object sender, EventArgs e)
        {//粘贴菜单
            this.richTextBox1.Paste();
        }

 

你可能感兴趣的:(C++,c,windows,C#)