Tuấn Đỗ
Tuấn Đỗ

Reputation: 21

How to catch keyboard enter key event in Windows Forms

I'm a beginner with C#. I have a button to login into my program. How to catch keyboard enter key event this program

Here is my code without enter event handler

private void btnLogin_Click(object sender, EventArgs e)
{
    var q = from m in db.Users
            where m.Use_Name == txtUserName.Text && m.Use_Password == txtPassword.Text
            select m;

    if (q.Any())
    {
        Main m1 = new Main();
        m1.Show();
        this.Visible = false;
    }
    else
    {
        MessageBox.Show("username or password is incorrect");
    }
}

Upvotes: 2

Views: 249

Answers (1)

Neel
Neel

Reputation: 11741

You can write below piece of code :-

 if (e.KeyChar == 13)
    {
        MessageBox.Show("Enter pressed", "Attention");                
    }

OR

You can designate a button as the "AcceptButton" in the Form's properties.

See this:- https://msdn.microsoft.com/en-us/library/aa984346(VS.71).aspx#Mtps_DropDownFilterText

Upvotes: 2

Related Questions