覆盖.NET RichTextBox上的ShortCut键

我正在使用RichTextBox(.NET WinForms 3.5),并希望覆盖一些标准的ShortCut键….例如,我不希望Ctrl + I通过RichText方法使文本斜体,但是而是运行我自己的方法来处理文本。

有任何想法吗?

Ctrl + I不是受ShortcutsEnabled属性影响的默认快捷方式之一。

以下代码拦截了KeyDown事件中的Ctrl + I,因此您可以在if块中执行任何操作,只需确保按下我所示的按键。

private void YourRichTextBox_KeyDown(object sender, KeyEventArgs e) { if ((Control.ModifierKeys & Keys.Control) == Keys.Control && e.KeyCode == Keys.I) { // do whatever you want to do here... e.SuppressKeyPress = true; } } 

将RichtTextBox.ShortcutsEnabled属性设置为true,然后使用KeyUp事件自行处理快捷方式。 例如

 using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.textBox1.ShortcutsEnabled = false; this.textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp); } void textBox1_KeyUp(object sender, KeyEventArgs e) { if (e.Control == true && e.KeyCode == Keys.X) MessageBox.Show("Overriding ctrl+x"); } } }