C# - Close Form without Stopping Application

Asked

Viewed 1,224 times

3

Hello, I’m using C#, to create a login system. And I would like to know some formula to close this login window without stopping the application. I used this. Hide(); to hide the form, but when closing the program it keeps running only hidden. Can anyone help me? Remembering that when typing the user and the correct password will call another form.

That’s the code I’m using:

if (usuarioTextBox.Text == "Administração" && senhaTextBox.Text == "@dmin321")
            {
               MessageBox.Show("Acesso Permitido");
               this.Hide();
               Form2 novoFormulario = new Form2();
               novoFormulario.Show();
            }
  • When closing the application, the form you had hidden with this.Hide(), keep rolling? Can you show me this?

  • @Vinicius I think when he says when closing the program it continues running in fact he wants to say when closing the second form, the first form continues running . I guess that’s it :-)

  • That’s right, Igor Venturelli

  • Cool @Tec.Alves . Have you seen my answer? I believe it’s what you need.

  • i would like the friend leo Longhi, the main form of the application is Form1, and before running it, call the formlogin and validate the user, if it is valid, just continue with the application, if not, close.

3 answers

2


You can change from .Show() for .ShowDialog() and close your form when the second form is disposed:

if (usuarioTextBox.Text == "Administração" && senhaTextBox.Text == "@dmin321")
{
    MessageBox.Show("Acesso Permitido");
    Hide();
    Form2 novoFormulario = new Form2();
    novoFormulario.ShowDialog();
    if(novoFormulario.IsDisposed)
        Close();
}

In addition, in the second form, it is necessary to call the method Dispose(); at the event FormClosing:

private void Form2_FormClosing(Object sender, FormClosingEventArgs e) 
{
    Dispose();
}

When calling the method Dispose(); you guarantee that all resources used by the second form have been closed/closed.

2

When you want to end the use of your application completely use the Application Exit, you can put it in the event Formclosed of the form(s) (s):

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }

In this way all applications will be closed.

2

Tip: Because instead of calling the login form first, it doesn’t start with the second one, so it can call the login and get login permission to continue! can change the first form to be called in Program.Cs

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form2 ());
    }

Exchanging Form1 for Form2

and in the form 2 load you can call by Form1(Login)

Visible = false;
Form1 Login= new Form1();
          Login.ShowDialog();

terminating with some condition of true or false to make or not the Visible form (if login is allowed)

Browser other questions tagged

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