The user will have 3 attempts to set the password

Asked

Viewed 386 times

-3

The following form does not open when the password is correct and if it is wrong continues to pass even if the password has typed 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();
        }
    }
  • Why are you wearing while ?

3 answers

0


The problem is that logic should not be within this method, it is a logic for the whole form and the control of attempts should be in the object and not in the method. Taking this variable out 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();
    }
}

I put in the Github for future reference.

0

Every time you run the function btnEntrar_Click(); the variable int erro=0; again gets the value zero. Try declaring the variable in the body of your class this way:

class Program
{
    static int erro;

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

Don’t forget to increment the variable if your algorithm identifies an error in authentication in this way:

erro++;

0

Why "keep going even if the password has typed 3 times".

Whenever the user presses the button that generates the event btnEntrar_Click(), right in the first line, you pass the 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. It is a failure on your part.

Removes this event variable from the button.

static int erro = 0;

And whenever the user misses the incremental keyword.

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 the erro=0 is not being reset to 0 in the button event.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.