2012-11-23 45 views

回答

0

自己嘗試一下:
打開編輯器,鍵入一些文字,標註一些這段文字,並按下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