Reputation: 11161
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
?
Upvotes: 4
Views: 2133
Reputation: 1079
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;
}
}
Upvotes: 0
Reputation: 4776
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);
}
}
Upvotes: 4
Reputation: 62130
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++.
Upvotes: 2
Reputation: 31249
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;
}
Upvotes: 1
Reputation: 176956
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
Upvotes: 1