Valerie Meunier

Valerie Meunier

  • 1.7k
  • 398
  • 16.1k

cannot go to Form2 if Form1 is closed

Jan 7 2022 1:19 PM

Hi

when i click on button1 in Form1, Form1 is closed but Form2 is not shown. If i remove Close(); then Form2 is open, but Form1 remains open. I want to close Form1 and go to Form2 (from Form2 to Form1 is ok, probably because Form1 is still open). How to fix that?
Thanks

public partial class Form1 : Form
    {
        public Form1()
        { InitializeComponent(); }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
            Form2 frm2 = new Form2();
            frm2.Show();
        }

  public partial class Form2 : Form
    {
        public Form2()
        { InitializeComponent(); }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
            Form1 frm1 = new Form1();
            frm1.Show();
        }

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


Answers (3)