[英]Wpf RichTextBox change key to line break;
我可以使用 Enter 鍵開始一個新行,但我可以使用 Ctrl+Enter 代替嗎?
我想用 Enter 鍵做其他事情。
我嘗試的 KeyDown 事件不起作用。
當我按 Enter 時,它總是開始一個新行。
您可以將屬性AcceptsReturn
的值設置為false
並通過為 PreviewKeyDown 事件創建一個事件處理程序來處理 ctrl + enter 鍵,如下所示:
(rtb 是 RichTextBox 的名稱)
private void RichTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && Keyboard.IsKeyDown(Key.LeftCtrl))
{
// do whatever you want
// if u want to add a new line just uncomment the next lines
// rtb.AppendText(Environment.NewLine);
// rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
e.Handled = true;
}
}
我覺得對於這個要求,你應該只使用回車鍵,它會根據我的要求工作
private void richTextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode==Keys.Enter )
{
//Enter key is down
//Capture the text in next line if you press enter only this event will
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.