The user will have 3 attempts to set the password

-3

The next form does not open when the password is correct and if it is wrong it continues even if the password has been entered 3 times.

private void btnEntrar_Click(object sender, EventArgs e)
    {
        int erro=0;

        if (senha != "")
        {
            while(erro<=2)
            { 
                String verifica = txtVerifica.Text;
                if (verifica == senha)
                {
                    if (Application.OpenForms.OfType<Desenvolvimento>().Count() > 0)
                    {
                        Form desenvolvimento = Application.OpenForms["Desenvolvimeto"];
                        desenvolvimento.Show();
                        this.Hide();
                    }
                    else
                    {
                        Desenvolvimento desenvolvimento = new Desenvolvimento();
                        desenvolvimento.Show();
                        this.Hide();
                    }
                }
                else
                {
                    if (erro == 3)
                    {
                        MessageBox.Show("Três tentativas !");
                        Application.Exit();
                    }
                    else
                    {
                        erro=erro+1;
                        MessageBox.Show("Senha inválida !");
                        break;
                    }
                }
            }
        }
        else
        {
            MessageBox.Show("Cadastre uma senha !");
            txtSenha.Focus();
        }
    }
    
asked by anonymous 25.05.2018 / 07:41

3 answers

0
The problem is that the logic should not be within this method, it is logic for the whole form and the control of attempts should be on the object and not on the method. Taking this variable works. And you can make several simplifications. Some things can still be improved.

private int tentativasErradas = 0;

private void btnEntrar_Click(object sender, EventArgs e) {
    if (senha != "") {
        if (txtVerifica.Text == senha) {
            Form desenvolvimento = Application.OpenForms.OfType<Desenvolvimento>().Any() ? Application.OpenForms["Desenvolvimeto"] : new Desenvolvimento();
            desenvolvimento.Show();
            this.Hide();
       } else {
            if (tentativasErradas == 3) {
                MessageBox.Show("Três tentativas !");
                Application.Exit();
            } else {
                tentativasErradas++;
                MessageBox.Show("Senha inválida !");
            }
        }
    } else {
        MessageBox.Show("Cadastre uma senha !");
        txtSenha.Focus();
    }
}
    
25.05.2018 / 14:14
0

The reason for "keeps going even if the password has been typed 3 times".

Whenever the user clicks on the button that generates the event btnEntrar_Click() , then on the first line, you pass erro = 0 , which makes it always the "first time". Even if the user is entering for the 100th time, the error will always be reset to 0. This is a failure on your part.

Remove this variable from the button event.

static int erro = 0;

And whenever the user misses the keyword increment.

Something like this.

class Program
{
    static int erro = 0;
    private void btnEntrar_Click(object sender, EventArgs e)
    {
        erro++;
        if(erro < 3)
        {
         //utilizador ainda não falhou 3 vezes
        }
        else
        {
         //utilizador falhou 3 vezes (ou mais)
        }
    }
}

As it turns out, the difference is that erro=0 is not being reset to 0 in the button event.

    
25.05.2018 / 10:13
0

Every time you run the btnEntrar_Click(); function, the int erro=0; variable again receives a value of zero. Try declaring the variable in the body of your class as follows:

class Program
{
    static int erro;

    static void Main(string[] args)
    {
        Menu();
    }
}

Do not forget to increment the variable if your algorithm identifies an authentication error like this:

erro++;
    
25.05.2018 / 08:15