WPF: A TextBox that has an event that fires when the Enter Key is pressed
Asked Answered
W

6

31

Instead of attaching a PreviewKeyUp event with each TextBox in my app and checking if the pressed key was an Enter key and then do an action, I decided to implement extended version of a TextBox that includes a DefaultAction event that fires when an Enter Key is pressed in a TextBox.

What I did was basically create a new Class that extends from TextBox with a public event DefaultAction, like such:

public class DefaultTextBoxControl:TextBox
{
    public event EventHandler<EventArgs> DefaultAction = delegate { };

    public DefaultTextBoxControl()
    {
        PreviewKeyUp += DefaultTextBoxControl_PreviewKeyUp;
    }

    void DefaultTextBoxControl_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key != Key.Enter)
        {
            return;
        }
        DefaultAction(this, EventArgs.Empty);
    }
}

I then use this custom textbox from my app like such (xaml):

<Controls:DefaultTextBoxControl  DefaultAction="DefaultTextBoxControl_DefaultAction">
</Controls:DefaultTextBoxControl>

Now in my little experience I've had in learning WPF I've realized that almost most of the time there is a "cooler" (and hopefully easier) way to implement things

...so my question is, How can I improve the above control? Or maybe is there another way I can do the above control? ...maybe using only declarative code instead of both declarative (xaml) and procedural (C#) ?

Wicopy answered 3/5, 2009 at 4:28 Comment(0)
K
46

Have a look at this blog post from a few months back where I attach a 'global' event handler to TextBox.GotFocus to select the text.

Essentially you can handle the KeyUp event in your App class, like this:

protected override void OnStartup(StartupEventArgs e)
{
    EventManager.RegisterClassHandler(typeof(TextBox),
        TextBox.KeyUpEvent,
        new System.Windows.Input.KeyEventHandler(TextBox_KeyUp));

    base.OnStartup(e);
}

private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key != System.Windows.Input.Key.Enter) return;

    // your event handler here
    e.Handled = true;
    MessageBox.Show("Enter pressed");
}

... and now every TextBox in your application will call the TextBox_KeyUp method as users type into them.

Update

As you've pointed out in your comment, this is only useful if every TextBox needs to execute the same code.

To add an arbitrary event like an Enter keypress, you might be better off looking into Attached Events. I believe this can get you what you want.

Kail answered 3/5, 2009 at 8:11 Comment(1)
But with this, how do you assign what happens when the enter key is pressed ? because every TextBox does something differentWicopy
T
39

Since this question was asked, there is now an InputBindings property on TextBoxes and other controls. With this, a purely XAML solution can be used, rather than using custom controls. Assigning KeyBindings for Return and Enter to point to a command can do this.

Example:

<TextBox Text="Test">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding SomeCommand}" Key="Return" />
        <KeyBinding Command="{Binding SomeCommand}" Key="Enter" />
    </TextBox.InputBindings>
</TextBox>

Some have mentioned that Enter does not always work, and Return may be used on some systems.

Tauten answered 21/12, 2014 at 16:25 Comment(3)
Looks nice so I tried it but this doesn't work. Using a button my command get's triggered with the textbox it doesn't. I even added a Source={StaticResource viewModel} but nothing works.Tacket
Managed to skip over Matthis Kohli's comment whilst reading this answer and applied it to my situation and lucky for me it did fire off the relevant methodPosture
UpdateSourceTrigger=PropertyChanged seems to be required.Prohibitory
I
12

When the user presses the Enter key in the TextBox, the input in the text box appears in another area of the user interface (UI).

The following XAML creates the user interface, which consists of a StackPanel, a TextBlock, and a TextBox.

<StackPanel>
  <TextBlock Width="300" Height="20">
    Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>

The following code behind creates the KeyDown event handler. If the key that is pressed is the Enter key, a message is displayed in the TextBlock.

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}

For more info read MSDN Doc

Ignominious answered 16/1, 2018 at 7:49 Comment(1)
thank you for including the C# side. All these other answers leave me with an incomplete program.Saxhorn
S
0

Otherwise you can do everything by c# like

//at the constructor, subscribe your textbox to the KeyUp event
YOUR_TEXTBOX.KeyUp += OnKeyUp;

//listen to the event
private void OnKeyUp(object sender, KeyEventArgs e)
{
   //when typing, listen to the "Enter" key
   if (e.Key == Key.Enter)
        Search(); //do something
}
Sentence answered 6/3, 2022 at 11:51 Comment(0)
G
-1
    private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
    {
        string etr = e.Key.ToString();

        if (etr == "Return")
        {
            MessageBox.Show("You Press Enter");
        }
    }
Graduate answered 3/10, 2017 at 2:8 Comment(0)
G
-2

add event in xaml to the specific textbox or object:

KeyDown="txtBarcode_KeyDown"

Graduate answered 3/10, 2017 at 2:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.