在富文本框中启用复制,剪切,过去的窗口

D P*_* P. 28 c# vb.net visual-studio

我的程序中有一个富文本框(richTextBox1),如下所示.但是当我右键单击它时,它不会弹出"复制,剪切,过去"窗口.你能否告诉我如何在我的Rich Text Box中启用这个"复制,剪切,过去"窗口?我是C#的新手,如果您知道如何解决这个问题,请一步一步告诉我

在此输入图像描述

Thi*_*a H 30

尝试使用此代码

UPDATED CODE:

        private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {   //click event
                //MessageBox.Show("you got it!");
                ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
                MenuItem menuItem = new MenuItem("Cut");
                menuItem.Click += new EventHandler(CutAction);
                contextMenu.MenuItems.Add(menuItem);
                menuItem = new MenuItem("Copy");
                menuItem.Click += new EventHandler(CopyAction);
                contextMenu.MenuItems.Add(menuItem);
                menuItem = new MenuItem("Paste");
                menuItem.Click += new EventHandler(PasteAction);
                contextMenu.MenuItems.Add(menuItem);

                richTextBox1.ContextMenu = contextMenu;
            }
        }
        void CutAction(object sender, EventArgs e)
        {
            richTextBox1.Cut();
        }

        void CopyAction(object sender, EventArgs e)
        {
            Graphics objGraphics;
            Clipboard.SetData(DataFormats.Rtf, richTextBox1.SelectedRtf);
            Clipboard.Clear();
        }

        void PasteAction(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText(TextDataFormat.Rtf))
            {
                richTextBox1.SelectedRtf
                    = Clipboard.GetData(DataFormats.Rtf).ToString();
            }
        } 
Run Code Online (Sandbox Code Playgroud)

如果您想使用记事本等其他应用程序复制粘贴,(without styles )请更换以下方法

       void CopyAction(object sender, EventArgs e)
        {
            Clipboard.SetText(richTextBox1.SelectedText);
        }

        void PasteAction(object sender, EventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                richTextBox1.Text
                    += Clipboard.GetText(TextDataFormat.Text).ToString();
            }
        }  
Run Code Online (Sandbox Code Playgroud)

  • @Thilina H很好的答案,但每次右键单击文本时,您都不需要创建上下文菜单.这是滞后和不方便的.相反,只需在Form_Load上创建并附加上下文菜单一次. (4认同)
  • 非常感谢您向我展示一个例子.为了使用这个代码,我还必须制作一个`MenuStrip`吗?因为当我使用你的代码`private void richTextBox1_MouseUp(object sender,MouseEventArgs e)`没有被调用.如果我双击原始图片中显示的`richTextBox1`,它将在`Form1.Designer.cs`和`Form1.cs`中自动生成代码: - `private void richTextBox1_TextChanged(object sender,EventArgs e)`Can你能告诉我怎样才能让我的程序为`private void richTextBox1_MouseUp(object sender,MouseEventArgs e)生成代码 (2认同)
  • 非常感谢您向我提供所有这些信息。如果我只在这个“richTextBox1”中做“复制、剪切、过去”的事情,它就可以工作。如果我从“richTextBox1”中剪切并在记事本中过去,它也可以工作。但是,如果我尝试从“richTextBox1”复制并将其粘贴到记事本中,则它不起作用。最重要的是,如果我想将记事本中的一些文本复制到这个 `richTextBox1`,代码将不起作用。非常感谢您关注此事。 (2认同)

Jae*_*aex 26

如果您有多个RichTextBox,则可以使用此扩展方法:

public static void AddContextMenu(this RichTextBox rtb)
{
    if (rtb.ContextMenuStrip == null)
    {
        ContextMenuStrip cms = new ContextMenuStrip()
        {
            ShowImageMargin = false
        };

        ToolStripMenuItem tsmiUndo = new ToolStripMenuItem("Undo");
        tsmiUndo.Click += (sender, e) => rtb.Undo();
        cms.Items.Add(tsmiUndo);

        ToolStripMenuItem tsmiRedo = new ToolStripMenuItem("Redo");
        tsmiRedo.Click += (sender, e) => rtb.Redo();
        cms.Items.Add(tsmiRedo);

        cms.Items.Add(new ToolStripSeparator());

        ToolStripMenuItem tsmiCut = new ToolStripMenuItem("Cut");
        tsmiCut.Click += (sender, e) => rtb.Cut();
        cms.Items.Add(tsmiCut);

        ToolStripMenuItem tsmiCopy = new ToolStripMenuItem("Copy");
        tsmiCopy.Click += (sender, e) => rtb.Copy();
        cms.Items.Add(tsmiCopy);

        ToolStripMenuItem tsmiPaste = new ToolStripMenuItem("Paste");
        tsmiPaste.Click += (sender, e) => rtb.Paste();
        cms.Items.Add(tsmiPaste);

        ToolStripMenuItem tsmiDelete = new ToolStripMenuItem("Delete");
        tsmiDelete.Click += (sender, e) => rtb.SelectedText = "";
        cms.Items.Add(tsmiDelete);

        cms.Items.Add(new ToolStripSeparator());

        ToolStripMenuItem tsmiSelectAll = new ToolStripMenuItem("Select All");
        tsmiSelectAll.Click += (sender, e) => rtb.SelectAll();
        cms.Items.Add(tsmiSelectAll);

        cms.Opening += (sender, e) =>
        {
            tsmiUndo.Enabled = !rtb.ReadOnly && rtb.CanUndo;
            tsmiRedo.Enabled = !rtb.ReadOnly && rtb.CanRedo;
            tsmiCut.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
            tsmiCopy.Enabled = rtb.SelectionLength > 0;
            tsmiPaste.Enabled = !rtb.ReadOnly && Clipboard.ContainsText();
            tsmiDelete.Enabled = !rtb.ReadOnly && rtb.SelectionLength > 0;
            tsmiSelectAll.Enabled = rtb.TextLength > 0 && rtb.SelectionLength < rtb.TextLength;
        };

        rtb.ContextMenuStrip = cms;
    }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它: richTextBox1.AddContextMenu();

截图: 截图

  • 一个非常完美的简单解决方案.谢谢.人们似乎真的过分复杂了...... (3认同)