Search code examples
c#visual-studiowinformstextboxkeydown

KeyDown removes input from my TextBox, how to stop this?


I'm making a login form in which I want to press the Enter key and I get a peroformedClick on my login button intitled BtnLogin.

private void txtPassword_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        BtnLogin.PerformClick();
    }
}

The issue is that when I press Enter key I lose input on my password TextBox intitled txtPassword text box.

How to stop this from happening?


Solution

  • I simply use the following code without the loss of the password after "ENTER". The code is as follows:

    private void textBox1_KeyDown(object sender, KeyEventArgs e) {
      if (e.KeyCode == Keys.Enter)
      {
        this.button1_Click(sender, e);
      }
    }
    

    But if I set the TextBox as a multi -line text, the password is lost after pressing "ENTER".(Actually the password is not lost, but it has been topped)

    enter image description here

    You need to click on the small triangle in the upper right corner of TextBox in the designer to cancel the "Multiline"

    enter image description here

    If this answer is not what you want, please provide a more complete code