4
votes

I'd like to be able to modify the behaviour of the wpf textbox without the need of implementing a new class.

I want an Excel like enter / Alt+Enter behavior, when the user hits "Enter" the textbox is validated (movefocus ...), but when he hits "ALT+Enter", the textbox has to add a new line (my textbox supports multiline : AcceptsReturn is true).

I've tried (in the textbox PreviewKeyDown event): - Raising to construct a KeyEventArgs and a TextCompositionEventArgs following this link : How can I programmatically generate keypress events in C#? - I've tried SendKeys.SendWait("{ENTER}") but it sends many new line commands

Is there a way to do this ?

Thank you

    private void m_MeasurementName_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        var tb = (sender as TextBox);

        if (Keyboard.Modifiers == ModifierKeys.Alt && Keyboard.IsKeyDown(Key.Enter))
        {
            // 1st try
            var key = "\n\r";
            var routedEvent = Keyboard.KeyDownEvent;
            tb.RaiseEvent(new TextCompositionEventArgs(InputManager.Current.PrimaryKeyboardDevice, new TextComposition(InputManager.Current, tb, key)) { RoutedEvent = routedEvent });

            // 2nd
            var key = Key.Enter;
            var routedEvent = TextCompositionManager.TextInputEvent;
            tb.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(tb), 0, key) { RoutedEvent = routedEvent });

            // 3rd
            System.Windows.Forms.SendKeys.SendWait("{ENTER}");

            // 4th Strangely works but not ... you know
            MessageBox.Show("ALT+ENTER");

            e.Handled = true;
        }
        else if (Keyboard.IsKeyDown(Key.Enter))
        {
            tb.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            e.Handled = true;
        }
    }
1
Can you please provide the code in textBox PreviewKeyDown routine to give us the chance to give you a better help?Fischermaen
Mh, can't you concatenate to current text a simple "\n" character?Francesco Belladonna

1 Answers

7
votes

If the user presses Alt+Enter, simply add a new line onto the existing TextBox.Text. If they hit just Enter, trigger an UpdateSource on the Text Binding

private void m_MeasurementName_PreviewKeyDown(object sender, KeyEventArgs e)
{
    var tb = (sender as TextBox);

    if (Keyboard.Modifiers == ModifierKeys.Alt && Keyboard.IsKeyDown(Key.Enter))
    {
        tb.Text += "\r\n";
        tb.SelectionStart = tb.Text.Length;

        e.Handled = true;
    }
    else if (Keyboard.IsKeyDown(Key.Enter))
    {
        var textBinding = BindingOperations.GetBindingExpression(
            tb, TextBox.TextProperty);

        if (textBinding != null)
            textBinding.UpdateSource();

        e.Handled = true;
    }
}

For the NewLine to work, make sure AcceptsReturn="True" on your TextBox