简体   繁体   English

WinForms ReadOnly TextBox按下Ctrl-R键

[英]WinForms ReadOnly TextBox Ctrl-R key down

I have a C# WinForms sample application, demonstrating interesting key handling issue. 我有一个C#WinForms示例应用程序,演示了有趣的密钥处理问题。 That's very simple: there is just a Form and TextBox on it. 这很简单:上面只有一个FormTextBox I set TextBox ReadOnly property to true . 我将TextBox ReadOnly属性设置为true

在此处输入图片说明

I have the next code in my Form : 我的Form有下一个代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData.HasFlag(Keys.R))
        {
            MessageBox.Show("There is 'R' key in KeyDown event");
        }
    }
}

When I press Ctrl - R keys, the MessageBox doesn't show up. 当我按Ctrl - R键时, MessageBox不会显示。 But if I set ReadOnly property of TextBox to true , the MessageBox appears. 但是,如果我将TextBox ReadOnly属性设置为true ,则会显示MessageBox The same thing happens, when I press Shift - R or Alt - R on ReadOnly TextBox . 当我在ReadOnly TextBox上按Shift - RAlt - R时ReadOnly发生同样的事情。

在此处输入图片说明

Any ideas, what is special with ReadOnly TextBox and Ctrl - R combination? 有什么想法, ReadOnly TextBoxCtrl - R组合有什么特别之处?

There is a patch in the TextBoxBase.ProcessCmdKey() method , it fixes a problem with certain short-cut keys still modifying the text when the control's ReadOnly property is set. TextBoxBase.ProcessCmdKey()方法中有一个修补程序,它解决了在设置控件的ReadOnly属性时某些快捷键仍在修改文本的问题。 They are Ctrl+R, Ctrl+J, Ctrl+E and Ctrl+L. 它们是Ctrl + R,Ctrl + J,Ctrl + E和Ctrl + L。

Afaik, this patch is a bit too crude, it should only apply to RichTextBox. Afaik,此补丁有点太粗糙了,只应应用于RichTextBox。

This problem is fixable by overriding the TextBox class and restore the normal behavior of these keys. 通过重写TextBox类并恢复这些键的正常行为可以解决此问题。 Add a new class to your project and paste the code shown below. 将新类添加到您的项目中,然后粘贴以下代码。 Compile. 编译。 Drop the new control from the top of the toolbox, replacing your original textbox. 从工具箱顶部放下新控件,替换原来的文本框。

using System;
using System.Windows.Forms;

class MyTextBox : TextBox {
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == (Keys.Control | Keys.R) ||
            keyData == (Keys.Control | Keys.L) ||
            keyData == (Keys.Control | Keys.E) ||
            keyData == (Keys.Control | Keys.J)) return false;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

It appears this is a known issue. 看来这是一个已知问题。 You'd have to ask Microsoft for the why of it... http://social.msdn.microsoft.com/Forums/en-US/941c9759-5531-49fe-9ebb-7fc6d812b0fd/ctrle-not-working-in-a-read-only-text-box?forum=csharplanguage 您必须向Microsoft询问其原因... http://social.msdn.microsoft.com/Forums/zh-CN/941c9759-5531-49fe-9ebb-7fc6d812b0fd/ctrle-not-working-in -a-只读文本框?论坛= csharplanguage

General advice: If you want to detect a specific character , then use KeyPress() . 一般建议:如果要检测特定字符 ,请使用KeyPress() It is called after translation from the keyboard key to the character set. 从键盘键转换为字符集后调用。

private void textBox1_KeyPress(Object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == 'R')
        MessageBox.Show("Hit an 'R'");
}

To handle keyboard "shortcuts", change the test: 要处理键盘“快捷键”,请更改测试:

if (ModifierKeys == Keys.Control && e.KeyChar == 'R')

For CTRL+R combination 对于CTRL + R组合

private void KeyDown(object sender, KeyEventArgs e)
{
  if (e.Control && e.KeyCode == Keys.R)
  {
    //your code
  }
}

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

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