Replace chunk of code C#

Asked

Viewed 75 times

-4

Guys, I have a question that I think you guys are gonna figure out.

I have a login form of this with the following condition:

    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox2.Text == "1234" || textBox3.Text == "1234")
        {
            PDVForm form = new PDVForm(textBox1.Text);
            this.Hide();
            form.Closed += (s, args) => this.Close();
            form.Show();
        }
        else
        {
            MessageBox.Show("Usuário ou Senha Incorreta", "Erro",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            return;
        }
    }

however I will have to make him change the if code:

        if (textBox2.Text == "1234" || textBox3.Text == "1234")

In the part of 1234 that is the login and the initial password, however if the user wants to change the part of 1234 what the code?

I’ll put it in the login form to get the text of another form, and so it will have the new login and password, but I can not make this code change the passage 1234

private void button1_Click(object sender, EventArg e)
{
   //Aqui vai o codigo de pegar o texto do campo textBox1
   //Aqui vai o codigo de pegar o texto do campo textBox2
   //Qual o codigo de mudar o 1234 para o texto digitado?
}

1 answer

2


You want to make login and password variable and stop being literal, but to do that, you need to take these variables from somewhere. If you want to take from another form, you will have to instantiate it and then get the properties of the fields.

It makes no sense for you to have a form for storing login and password that will later be used in a login form. Usually, login and password are written in databases, files or constant variables.

Besides, the logic of your if is wrong. You should use the operator and (&) instead of or else (||), because in this case, if only one of the fields is correct, it is already understood as a successful login.

private void button1_Click(object sender, EventArg e)
{
    /*  
        Exemplo caso queira usar constantes
        Constantes não podem ter seus valores alterados.
    */
    const string loginUser = "1234";
    const string loginPass = "5678";

    /*
        Exemplo caso queira usar arquivos
        O texto das senhas requerem que os arquivos existam na mesma pasta do executável.
    */
    string loginUser = System.IO.File.ReadAllText("usuario.txt");
    string loginPass = System.IO.File.ReadAllText("senha.txt");

    if (textBox2.Text == loginUser && textBox3.Text == loginPass)
    {
        PDVForm form = new PDVForm(textBox1.Text);
        this.Hide();
        form.ShowDialog();
        this.Close(); // será chamado quando "form" for fechado
    }
    else
    {
        MessageBox.Show("Usuário ou Senha Incorreta", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

In the example above, you should only use one of the cases. You can implement others if you want. If you still want to get the information from a form, please instate it and get its values as you did on PDVForm form = new PDVForm();.

  • If you’ve solved your problem, consider marking this answer as correct, just below the voting buttons.

  • 1

    Consider using the operator &&, in place of &. In this case it is not necessary to evaluate the 2nd part if the 1st part is already false. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators#conditional-Logical-and-Operator-

  • @tvdias thanks for the suggestion.

Browser other questions tagged

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