How to override copy and paste in richtextbox

前端 未结 3 1766
北恋
北恋 2021-01-02 18:00

How can I override the copy/paste functions in a Richtextbox C# application. Including ctrl-c/ctrl-v and right click copy/paste.

It\'s WPF richtextBox.

3条回答
  •  旧时难觅i
    2021-01-02 18:12

    To override the command functions:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    {
      if (keyData == (Keys.Control | Keys.C))
      {
        //your implementation
        return true;
      } 
      else if (keyData == (Keys.Control | Keys.V))
      {
        //your implementation
        return true;
      } 
      else 
      {
        return base.ProcessCmdKey(ref msg, keyData);
      }
    }
    

    And right-clicking is not supported in a Winforms RichTextBox

    --EDIT--

    Realized too late this was a WPF question. To do this in WPF you will need to attach a custom Copy and Paste handler:

    DataObject.AddPastingHandler(myRichTextBox, MyPasteCommand);
    DataObject.AddCopyingHandler(myRichTextBox, MyCopyCommand);
    
    private void MyPasteCommand(object sender, DataObjectEventArgs e)
    {
        //do stuff
    }
    
    private void MyCopyCommand(object sender, DataObjectEventArgs e)
    {
        //do stuff
    }
    

提交回复
热议问题