2008-11-04 33 views
1

我正在使用RichTextBox(.NET WinForms 3.5)並且想要重寫某些標準快捷鍵......例如,我不希望Ctrl + I通過RichText方法使文本斜體,而是運行我自己的方法來處理文本。覆蓋.NET RichTextBox上的ShortCut鍵

任何想法?

回答

4

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; 
    } 
} 
3

將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"); 
     } 
    } 
}