【问题标题】:Wpf RichTextBox change key to line break;Wpf RichTextBox 将键更改为换行符;
【发布时间】:2019-10-14 15:19:15
【问题描述】:

我可以使用 Enter 键开始一个新行,但我可以使用 Ctrl+Enter 代替吗?

我想用 Enter 键做其他事情。

我尝试的 KeyDown 事件不起作用。

当我按 Enter 时总是开始一个新行。

【问题讨论】:

  • 你能显示你的 KeyDown 事件处理程序的代码吗?另外,你为什么不试试 KeyUp 在你按键之后(而不是在按键期间)触发。

标签: c# .net wpf


【解决方案1】:

您可以将属性 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;
    }
}

【讨论】:

【解决方案2】:

我觉得对于这个要求你应该只使用输入键它会根据我的工作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    相关资源
    最近更新 更多