public void BtnLogin_Click(object sender, EventArgs e) { if (!loggedIn) { while (attempt <= MaxAttempts) { if (TxtPw.Text != myPassword && TxtUser.Text != username) { MessageBox.Show("Incorrect login," + (MaxAttempts - attempt) + " attempts remaining"); attempt++; return; } else if (TxtUser.Text != username) { // username is incorrect MessageBox.Show("Invalid username, " + (MaxAttempts - attempt) + " attempts remaining"); attempt++; return; } else { // username is correct // so check password if (TxtPw.Text != myPassword) { // Incorrect password MessageBox.Show("Incorrect password," + (MaxAttempts - attempt) + " attempts remaining"); attempt++; return; } else { //Both are correct attempt = 1; // reset the number of attempts MessageBox.Show("Login successful"); loggedIn = true; BtnLogin.Text = "Logout"; // this.Width = 1600; break; // come out of while loop }//endif }//endif }//end while } else { BtnLogin.Text = "Login"; loggedIn = false; TxtUser.Clear(); TxtPw.Clear(); } }