简体   繁体   English

Wpf RichTextBox 将键更改为换行符;

[英]Wpf RichTextBox change key to line break;

I can use Enter key to start a new line but can I use Ctrl+Enter instead?我可以使用 Enter 键开始一个新行,但我可以使用 Ctrl+Enter 代替吗?

I want to use Enter key to do other thing.我想用 Enter 键做其他事情。

The KeyDown event I tried not work.我尝试的 KeyDown 事件不起作用。

it is always start a new line when I press Enter.当我按 Enter 时,它总是开始一个新行。

You can set the value for property AcceptsReturn to false and handle the ctrl + enter key by creating an event handler for PreviewKeyDown event like this:您可以将属性AcceptsReturn的值设置为false并通过为 PreviewKeyDown 事件创建一个事件处理程序来处理 ctrl + enter 键,如下所示:

(rtb is name of the RichTextBox) (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;
    }
}

i feel like for this requirement you should use only enter key it will work according to me我觉得对于这个要求,你应该只使用回车键,它会根据我的要求工作

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM