Detect Enter Key C#

后端 未结 4 1803
无人及你
无人及你 2021-01-17 15:24

I have the following code which does not show the MessageBox when enter/return is pressed.

For any other key(i.e. letters/numbers) the MessageBox shows False.

<
4条回答
  •  情话喂你
    2021-01-17 16:19

    private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
     {
                if (e.Key == Key.Enter)
                {
                    MessageBox.Show("Enter key pressed");
                }
                else if (e.Key == Key.Space)
                {
                    MessageBox.Show("Space key pressed");
                }
    }
    

    Use PreviewKeyDown event to detect any key before shown in textbox or input

提交回复
热议问题