自己尝试一下:
打开编辑器,键入一些文字,标注一些这段文字,并按下N
。怎么了?标记的文本被替换为n
。
同样的事情发生在你的RichTextBox
。重要的是,在你设置的事件中,你只需添加一些功能,并保持默认的事件处理(由OS处理)。
所以你的代码,在一个按键,你只是做
richTextBox1.Select(1, 3);
其中选择一些人物和事后默认的事件处理踢,因此存在而它与N
更换了一些标记文本。
因此,您只需将事件标记为由您自己处理。不使用Handled
属性,但使用SuppressKeyPress
属性。
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
e.SuppressKeyPress = true;
}
}
的documentation of Handled
明确指出:
If you set Handled to true on a TextBox, that control will
not pass the key press events to the underlying Win32 text
box control, but it will still display the characters that the user typed.
这里是official documentation of SuppressKeyPress
。