How to override cut,copy,paste event of rad richtextbox

6 posts, 0 answers
  1. Amit
    Amit avatar
    5 posts
    Member since:
    Oct 2011

    Posted 18 Oct 2011 Link to this post

    Hi,

    I want to override cut,copy,paste event of richetextbox.
    Is it any way to implement it?

    Please share.

    Regards,
    Amit Nadhe
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 21 Oct 2011 Link to this post

    Hello Amit,

    You cannot override the default clipboard commands.
    However, you can disable their execution and wire in your implementations of the commands.
    You can find an example of how to create a custom command and wire it with RadRichTextBoxRibbonUI in this forum thread. You can also build a custom ContextMenu that executes your commands. For more information on how to do that, you can refer to this article.
    Lastly, the clipboard commands can be executed using the keyboard shortcuts (Ctrl + C, Ctrl + V, Ctrl + X).
    There is a list of the keyboard shortcuts and the commands they trigger, as well as possible customization options in this article
    I hope this helps.

    Regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. DevCraft banner
  4. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 30 Aug 2012 Link to this post

    Hi,

    I have a similar issue. I don't want to override the cut / copy / paste commands, rather I want to trigger another event when the cut command is executed so that I can run some check methods.

    Is there a way to raise an event such as Preview Cut?
  5. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 31 Aug 2012 Link to this post

    Hi Rob,

    There is an event CommandExecuting, which is fired when any command is triggered. You can check if the command that has caused the triggering of the event is Cut and interrupt its execution if some criteria are matched:

    private void editor_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
    {
        if (e.Command is CutCommand && shouldInterrupt)
        {
            e.Cancel = true;
        }
    }

    For simplicity, I have used a hypothetical shouldInterrupt boolean field. You can either use a field or perform some checks in the handler of the event.

    I hope this helps.

    Kind regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Robert
    Robert avatar
    271 posts
    Member since:
    Mar 2011

    Posted 31 Aug 2012 Link to this post

    Thanks Iva, that works perfectly.

    I just needed to assign the event like so:
    this.editor.CommandExecuting += editor_CommandExecuting;


    One last question. How do I access the clipboard data? What I want to do is loop through the clipboard data before it is pasted to check if it contains any annotations with specific property values.

    I can obviously capture the event like so:
    if (e.Command is PasteCommand)
    {
    }

    Thanks again for your time and help.

    All the best,

    Rob
  7. Nikolay Demirev
    Admin
    Nikolay Demirev avatar
    103 posts

    Posted 04 Sep 2012 Link to this post

    Hi Rob,

    You can use this code snippet to get the document fragment from the clipboard:

    if (e.Command is PasteCommand)
    {
        DocumentFragment fragment = ClipboardEx.GetDocument();
        if (fragment != null)
        {
            // loop through clipboard data
        }
    }

    DocumentFragment has mostly the same structure of a RadDocument and you can use the EnumerateChildrenOfType<YourAnnotationRangeStart>() method to get all annotation starts and check their properties.

    I hope this helps.

    Kind regards,
    Nikolay Demirev
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner