Detect Enter Key C#

后端 未结 4 1825
无人及你
无人及你 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:11

    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();
        }
    

提交回复
热议问题