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

后端 未结 5 1741
走了就别回头了
走了就别回头了 2021-01-30 18:10

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

相关标签:
5条回答
  • 2021-01-30 18:35

    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

    0 讨论(0)
  • 2021-01-30 18: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.

    0 讨论(0)
  • 2021-01-30 18:42

    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.

    0 讨论(0)
  • 2021-01-30 18:43
        private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
        {
            string etr = e.Key.ToString();
    
            if (etr == "Return")
            {
                MessageBox.Show("You Press Enter");
            }
        }
    
    0 讨论(0)
  • 2021-01-30 18:43

    add event in xaml to the specific textbox or object:

    KeyDown="txtBarcode_KeyDown"

    0 讨论(0)
提交回复
热议问题