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

后端 未结 5 1740
走了就别回头了
走了就别回头了 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.

    
      
        Type some text into the TextBox and press the Enter key.
      
      
      
    
    

    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

提交回复
热议问题