Reputation: 325
my code is :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key==Key.LeftCtrl && e.Key==Key.C) || (e.Key==Key.RightCtrl && e.Key==Key.C))
{
MessageBox.Show("Copy not allowed !");
e.Handled = true;
}
}
or , another way
, i have tried is :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key==Key.C) && (Keyboard.Modifiers==ModifierKeys.Control))
{
MessageBox.Show("Copy not allowed !");
e.Handled = true;
}
}
But they do not work !
Please do not tell me to set Focusable="False"
or IsHitTestVisible="False"
because after that , i can not use scrollbar !
Please help. thanks.
Upvotes: 1
Views: 2923
Reputation: 2593
I assume your problem is not really how to disable ctrl+A and ctrl+C, just because you don't want the user to do exactly that, but to prevent the user from copying the content of the text box. Problem is, Ctrl+A Ctrl+C is not the only way to copy data. The user could select the text, and right click.
So what you should do then is not to override the keystroke, but the actual command that is being fired. (You may want to read up on how Commands works in WPF.)
To do that, simply add the following method to your class
private void CancelCopyCommand(object sender, DataObjectEventArgs e)
{
MessageBox.Show("Copy not allowed !");
e.CancelCommand();
}
And in the Constructor, register the command as follow: DataObject.AddCopyingHandler(richTextBox1, CancelCopyCommand);
Upvotes: 3
Reputation: 9394
You have to subscribe for the PreviewKeyDown-Event and in the Handler you have to set e.Handled = true
if your Key-Combination was pressed.
Upvotes: 0
Reputation: 659
A richTextbox is for entering Text, ok you can make it readonly, so why do you want to prevent copy?
Try using Images to prevent copy, or disable focusing/selecting. If the user selects Text, destroy the selection.
Upvotes: 0
Reputation: 69959
You can handle the PreviewKeyDown
event... you almost had it, you just needed to and (&) the Keyboard.Modifiers
because it could contain more than just ModifierKeys.Control
:
private void PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) ==
ModifierKeys.Control)
{
MessageBox.Show("CTRL + C was pressed");
}
}
Upvotes: 5