Call a form and close a form in the same event

1

How do I call a form and close a form in the same event. For example:

private void iniciar_Click(object sender, EventArgs e)
{
    Close();
    Frm1 newForm2 = new Frm1();
    newForm2.ShowDialog();  
}
    
asked by anonymous 12.03.2014 / 04:50

3 answers

5

This is done by placing the second form in a Thread:

public static void ThreadProc()
{
    Application.Run(new Frm1());
}

private void iniciar_Click(object sender, EventArgs e)
{
    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
    t.SetAppartmentState(ApartmentState.STA);
    t.IsBackground = true;
    t.Start();
    this.Close();
}
    
12.03.2014 / 05:05
2

Hello, I was having the same problem as our colleague @AndersonBP mentioned, there searching the internet, I found a response from @JuniorTunjr that was easier to understand and solved my problem.

    private void btnIniciar_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form f = new frm2();
        f.Closed += (s, args) => this.Close(); 
        f.Show();
    }
    
12.06.2017 / 01:35
0

Use the this.Hide (); to close your LOGIN screen, as in the example below.

private void iniciar_Click(object sender, EventArgs e)

{

Frm1 newForm2 = new Frm1();
this.Hide(); // use dessa maneira.
newForm2.ShowDialog();  

}

    
10.04.2018 / 03:48