Frage

I'm having a weird problem right now with a log in form in my program. Basically if the user enters an incorrect login, and then hits enter in the popup, it closes it and submits the log in form again. Is there a way I can prevent this? So when the user presses enter to close the pop up, the enter press doesn't register on my log in form?

Heres the code from my login form. Button1 is the log in button Thanks!

private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox2.Text == "")
        {
            MessageBox.Show("Please enter a username and password!");
        }
        else
        {
            bool loggedin = false;
            for (int i = 0; i <= Program.ProfileList.Count - 1; i++)
            {
                if (Program.ProfileList[i].Username == textBox1.Text)
                {
                    AESEncrypt encrypt = new AESEncrypt();
                    if (Program.ProfileList[i].Password == encrypt.EncryptToString(textBox2.Text))
                    {
                        MessageBox.Show("Succesfully logged in!");
                        Program.ActiveUser = textBox1.Text;
                        loggedin = true;
                        this.Close();
                    }
                }
            }
            if (!loggedin)
            {
            MessageBox.Show("Incorrect username or password!");
            }

        }
    }

    private void textBox2_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            button1.PerformClick();
        }
    }
War es hilfreich?

Lösung

This happened because you used the KeyUp event. And also pressed the Enter key to dismiss the message box. Which does it correctly, it uses the KeyDown event. So the dialog closes, your TextBox gets the focus back and you release the Enter key. To trigger the KeyUp event again :)

Also turn on your speakers to hear the nasty DING! you get from the text box not liking the Enter key.

Use the KeyDown event instead:

    private void textBox2_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyCode == Keys.Enter) {
            button1.PerformClick();
            e.Handled = e.SuppressKeyPress = true;
        }
    }

Andere Tipps

When this problem happens, is focus in the TextBox represented by textBox2? This is probably what is happening:

  1. The user enters an incorrect username and password.
  2. The user presses Enter with focus in the password field.
  3. A MessageBox is displayed, indicating the username and password are incorrect.
  4. The user presses enter to dismiss the MessageBox.

I'm not positive on this, but most likely the MessageBox is closed when the key down event for the enter key is received. Focus changes back to the login form, specifically to the password field (where focus was before the MessageBox was displayed). The key up event is sent to the control in focus, which is the password field - which triggers the log in check again.

To fix this, remove the key up handler and set the Form.AcceptButton property to the OK / Login button, and set the Form.CancelButton property to the Cancel button. This will automatically allow enter to press the OK / Login button, and Escape to press the Cancel button.

You can solve it as follows.You will also have to set the KeyPreview property to true.

Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.

        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text.Trim()) || string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("Please enter a username and password!");
            }
            else if (IsValidUser(textBox1.Text.Trim(), textBox2.Text.Trim()))
            {
                MessageBox.Show("Succesfully logged in!");
                Program.ActiveUser = textBox1.Text;
                this.Close();
            }
            else
            {
                MessageBox.Show("Incorrect username or password!");
            }
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }

        private bool IsValidUser(string userName, string userPassword)
        {
            for (int i = 0; i <= Program.ProfileList.Count - 1; i++)
            {
                if (Program.ProfileList[i].Username == userName)
                {
                    AESEncrypt encrypt = new AESEncrypt();
                    if (Program.ProfileList[i].Password == encrypt.EncryptToString(userPassword))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return false;
        }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top