4

Is it somehow possible to prevent end users from copying text from RichTextBox in a C# application? Maybe something like adding a transparent panel above the RichTextBox?

4
  • 1
    please describe your goal more clearly... and also how secure this needs to be ? which depends on the capabilities of the user you want to defend against... Commented Dec 29, 2011 at 9:04
  • @Yahia Given the goal he thinks he has, he is describing it pretty clearly. What you mean is that he should not be asking this question, but rather a different one. But if he changes his question, then all answers so far will be invalid. So, I do not think that we should be encouraging people to change questions. Perhaps we should be telling them to ask different ones. Commented Dec 29, 2011 at 9:16
  • 1
    Same way as for TextBox: stackoverflow.com/questions/5113722/… Commented Dec 29, 2011 at 9:23
  • You could disable the textbox. Commented Dec 29, 2011 at 10:24

5 Answers 5

4

http://www.daniweb.com/software-development/csharp/threads/345506

Re: Disable copy and paste control for Richtextbox control Two steps to be followed to disable the Copy-Paste feature in a textbox,

1) To stop right click copy/paste, disable the default menu and associate the textbox with an empty context menu that has no menu items.

2) To stop the shortcut keys you'll need to override the ProcessCmdKey method:

C# Syntax (Toggle Plain Text)

private const Keys CopyKey = Keys.Control | Keys.C;
private const Keys PasteKey = Keys.Control | Keys.V;

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { 
    if((keyData == CopyKey) || (keyData == PasteKey)){
        return true;
    } else {
      return base.ProcessCmdKey(ref msg, keyData);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You need to subclass the rich edit box control, override the WM_COPY message, and do nothing when you receive it. (Do not delegate to DefWindowProc.)

You will also have to do the same for WM_CUT.

And still anyone will be able to get the text using some utility like Spy++.

1 Comment

This would be the most complete solution
1

One way to doing it is make access to clipbord object and than you can manitpulate content of clipbord.

method for clearing clipbord is : Clipboard.Clear Method

Comments

1

You could do this (I think):

//Stick this in the KeyDown event of your RichtextBox
    if (e.Control && e.KeyCode == Keys.C) {
        e.SuppressKeyPress = true;
    }

1 Comment

This is not correct because there are many ways to perform a copy operation, and Ctrl+C is just one of them.
0

Another -very elegant- working option (which also prevents context-menu actions).

The XAML:

<RichTextBox CommandManager.PreviewExecuted="richTextBox_PreviewExecuted"/>

The code-behind:

private void richTextBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Command == ApplicationCommands.Copy ||
        e.Command == ApplicationCommands.Cut  ||
        e.Command == ApplicationCommands.Paste)
    {
        e.Handled = true;
    }
}

(Source: https://programmingistheway.wordpress.com/2016/09/07/disable-wpf-textbox-cut-copy-and-paste/)

Also, if it's of interest, you can "convert" the "Cut" command into a "Copy" one (in order to allow copying but aviod moficiation):

private void richTextBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
{
    if (e.Command == ApplicationCommands.Paste)
    {
        e.Handled = true;
    }
    else if (e.Command == ApplicationCommands.Cut)
    {
        ApplicationCommands.Copy.Execute(e.Parameter, (IInputElement)sender);
        e.Handled = true;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.