Detect Enter Key C#

我怕爱的太早我们不能终老 提交于 2019-12-01 16:22:41

This is because when you press Enter TextChanged event won't fire.

in your form designer class (formname.designer.cs) add this :

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Login_KeyPress);

and add this code to backbone code (formname.cs):

void Login_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)13)
            MessageBox.Show("ENTER has been pressed!");
        else if (e.KeyChar == (char)27)
            this.Close();
    }
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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!