Problem in Dialogresult having to press the button 3 times to open

Asked

Viewed 115 times

0

I’m having a serious recurrence problem with Dialogresult.

Tela de Login

This is the Login screen, thanks to the use of Dialogresult, there is a strange error, you have to press Login twice. The first one picks up that Dialogresult is OK but for there, it does not return and tests, you have to click a second time for it to proceed.

Here’s the button code:

private void btnLogar_Click(object sender, EventArgs e)
    {

        DALConexao cx = new DALConexao(DadosDaConexao.StringDeConexao);

        ModeloLogin.Login = txtLogin.Text;
        ModeloLogin.Senha = txtSenha.Text;

        SqlConnection conexao = new SqlConnection(cx.StringConexao);
        SqlCommand cmd = new SqlCommand("Select login, senha FROM registro WHERE login = '" + ModeloLogin.Login + "'AND senha = '" + ModeloLogin.Senha + "'",conexao);
        conexao.Open();

        bool Verifica = cmd.ExecuteReader().HasRows;
        if (Verifica == true)
        {
            btnLogar.DialogResult = DialogResult.OK;
            count = count + 1;
            if (count > 1) 
            {
            conexao.Close();
            MessageBox.Show("Login efetuado com sucesso! Bem-vindo.", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Exclamation );
            this.IsNewLogged = true;
            conexao.Open();
            SqlCommand cmd2 = new SqlCommand("select nome from registro where login = '"+ ModeloLogin.Login + "'",conexao);
            ModeloLogin.Nome = Convert.ToString(cmd2.ExecuteScalar());
            conexao.Close();
            SqlCommand cmd3 = new SqlCommand("select permissao from registro where login = '" + ModeloLogin.Login + "'",conexao);
            conexao.Open();
            ModeloLogin.Permissao = Convert.ToString(cmd3.ExecuteScalar());
            }
        }
        else
        {
            MessageBox.Show("Login ou senha incorretos! Tente novamente.", "Erro", MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
        conexao.Close();
    }

And here’s the code to Program.Cs:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        frmLogin f = new frmLogin();
        if (f.ShowDialog() == DialogResult.OK)
        {
            frmDecisao d = new frmDecisao();
            if(d.ShowDialog() == DialogResult.Yes)
            {
            Application.Run(new frmPrincipal());
            f.Dispose();
            d.Dispose();
                }
            if (d.ShowDialog() == DialogResult.No)
            {
                Application.Run(new frmPrincipal2());
                f.Dispose();
                d.Dispose();
            }
        }
    }
}

Originally he was supposed to make the Login click a Dialogresult OK and return to Program.Cs and test it on condition,however the first time he is clicked he stops in Dialogresult OK and in the second he continues and returns to the program.Cs someone knows tell me what is happening?

1 answer

1


In the first clickVoce this setting the dialogresult of the button, in the second click, the button returns the value.

Use this.dialogresult = .... To set direct on the return form

  • Thank you so much for the help,.

Browser other questions tagged

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