How to open Form2 and close Form1 in the same time by C#?

I have a problem trying to open form 2 from form 1 and close form 1, and I have tried these solutions and can't find the right one: Solution 1:-

Form2 form2 = new Form2();
            form2.Show();
            this.Close(); //close Form1

Solution 2:- in file Program.cs

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MyApplicationContext context = new MyApplicationContext();
        Application.Run(context);
    }
    public class MyApplicationContext : ApplicationContext
    {
        private Form1 form1 = new Form1();

        public MyApplicationContext()
        {
            form1 = new Form1();
            form1.Show();
        }
    }

and in Form1.cs

Form1 form1 = new Form1();
            form1.Show();
            this.Close();

This solution works but after closing the applications, I find that it still works in the background.

How can I fix this?


you can try this code:

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