Enable copy, cut, past window in a rich text box

前端 未结 7 1379
礼貌的吻别
礼貌的吻别 2021-01-31 09:30

I have a rich text box(richTextBox1) in my program as shown bellow. But when I right click on it, it doesn\'t pop up a “copy, cut, past” window. Can you please tell

7条回答
  •  梦谈多话
    2021-01-31 09:43

    I think the solution provided by Thilina H is excellent except few bugs.

    1. MouseUp event causes the right click to start after second click. I recommend using MouseDown event instead of MouseUp event.

    2. I tested secondly provided CopyAction method. In my case CopyAction method didn't copy enter characters. I had to edit the code like this:

      Clipboard.SetText(richTextBox1.SelectedText.Replace("\n", "\r\n"));
      
    3. When richTextBox1.SelectedText is empty the program showed an exception. I simply edited the CopyAction method shown by below to fix the issue.

          if (chatBox.SelectedText != null && chatBox.SelectedText != "")
          {
              Clipboard.SetText(richTextBox1.SelectedText.Replace("\n", "\r\n"));
          }
      

    Happy Coding!

提交回复
热议问题