How to override copy and paste in richtextbox

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

This is WPF richtextBox.

+7
source share
3 answers

To override 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 Winforms RichTextBox

- EDIT -

Implemented too late, this is a WPF question. To do this in WPF, you will need to add a special 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 } 
+17
source

What about Cut when using Copy and Paste Handlers? When you have a custom OnCopy implementation and you process it with

 e.Handled = true; e.CancelCommand(); 

OnCopy is also called when Cut is executed - I cannot find a way to find out if the method was called to perform copy or cut.

+4
source

I used this:
//doc.Editor - RichtextBox

  DataObject.AddPastingHandler(doc.Editor, new DataObjectPastingEventHandler(OnPaste)); DataObject.AddCopyingHandler(doc.Editor, new DataObjectCopyingEventHandler(OnCopy)); private void OnPaste(object sender, DataObjectPastingEventArgs e) { } private void OnCopy(object sender, DataObjectCopyingEventArgs e) { } 
+1
source

All Articles