简体   繁体   English

如何在WPF中的RichTextBox中禁用Ctrl + A和Ctrl + C?

[英]how to disable ctrl+A and ctrl+c in richtextbox in wpf?

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 : 或者,我尝试过的another way是:

    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" 请不要告诉我设置Focusable="False"IsHitTestVisible="False"

because after that , i can not use scrollbar ! 因为在那之后,我不能使用滚动条!

Please help. 请帮忙。 thanks. 谢谢。

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 : 您可以处理PreviewKeyDown事件...您几乎已经拥有了它,您只需要添加 (&) Keyboard.Modifiers因为它不仅可以包含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");
    }
}

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. 我认为您的问题不是真正禁用ctrl + A和ctrl + C的原因,只是因为您不希望用户这样做,而是阻止用户复制文本框的内容。 Problem is, Ctrl+A Ctrl+C is not the only way to copy data. 问题是,Ctrl + A Ctrl + C不是复制数据的唯一方法。 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.) (您可能想了解命令在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); 然后在构造函数中,按如下所示注册命令:DataObject.AddCopyingHandler(richTextBox1,CancelCopyCommand);

A richTextbox is for entering Text, ok you can make it readonly, so why do you want to prevent copy? richTextbox用于输入文本,好的,您可以将其设为只读,那么为什么要防止复制?

Try using Images to prevent copy, or disable focusing/selecting. 尝试使用图像防止复制,或禁用聚焦/选择。 If the user selects Text, destroy the selection. 如果用户选择“文本”,则销毁选择。

您必须订阅PreviewKeyDown-Event,并且在处理程序中,如果按下了键组合,则必须设置e.Handled = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM