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

0

I'm having a serious recurrence problem with DialogResult.

ThisistheLoginscreen,thankstotheuseofDialogResult,thereisastrangeerror,youhavetopressLogartwice.ThefirstoneitpicksupthatDialogResultisOKbutforthere,itdoesnotreturnandtests,youhavetoclickasecondtimeforittocontinue.

Hereisthebuttoncode:

privatevoidbtnLogar_Click(objectsender,EventArgse){DALConexaocx=newDALConexao(DadosDaConexao.StringDeConexao);ModeloLogin.Login=txtLogin.Text;ModeloLogin.Senha=txtSenha.Text;SqlConnectionconexao=newSqlConnection(cx.StringConexao);SqlCommandcmd=newSqlCommand("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 is the code for 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 it was for him to do that the click of the Logar is a DialogResult OK and return to the Program.cs and test it in the condition, however the first time it is clicked it into the DialogResult OK and in the second it continues and returns to program.cs anyone know tell me what's going on?

    
asked by anonymous 01.03.2017 / 16:19

1 answer

1

In the first click you are defining the button's dialogresult, in the second click, the button returns the value.

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

    
18.03.2017 / 23:19