C # - How to close the login form (initial form of the program) after calling another form?

2

I've been able to do the whole database part of the login form, so if the user enters login / password with data that is in the database, he can access.

But when I open the other form, the login does not close and the most I could do was hide it with this.visible = false;

I want the second form to open and the login to close without the whole application crashing, since with this.close () / close () everything would close!

My login button code looks like this:

Note: "access" is a Boolean variable that I assign when I was creating the login part with login / password of the program database.

if (acessar == true)
        {
            MessageBox.Show("Logado com sucesso!");
            this.Visible = false;

            Form2 novaform = new Form2();
            novaform.ShowDialog();

        }

        else
        {
            MessageBox.Show("Erro ao logar");          
        }
    
asked by anonymous 30.11.2015 / 00:41

2 answers

2

Take a look at this post: link

Basically, do the following when showing your Form2:

this.Hide();
var form2 = new Form2();
form2.Closed += (s, args) => this.Close(); 
form2.Show();

The first form is "hidden" until the second is closed, firing the Closed event that will actually close the first one.

    
30.11.2015 / 00:50
0

I've done something like this in my program.

//dentro da classe base do programa
public static bool reconect = true;
public static Usuario usuario = null;

//metodo Main()
while(reconect)
{
    reconect = false;
    Application.Run(new Login());
    // a variavel usuario é do tipo static, e é modificada quando o login é efetuado;
    if (usuario != null)
    {
        //caso seja feito o logoff, o usuario é setado em null e reconect para true.
        //se a TelaPrincipal ou Login for fechada ela automaticamente quebra o loop
        Application.Run(new TelaPrincipal());
    }
}
    
30.11.2015 / 01:53