user666
user666

Reputation: 318

Wpf RichTextBox change key to line break;

I can use Enter key to start a new line but can I use Ctrl+Enter instead?

I want to use Enter key to do other thing.

The KeyDown event I tried not work.

it is always start a new line when I press Enter.

Upvotes: 0

Views: 852

Answers (2)

Khadar
Khadar

Reputation: 79

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

Upvotes: 1

eren
eren

Reputation: 810

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:

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

Upvotes: 5

Related Questions