How do I prevent the app from terminating when I close the startup form?

后端 未结 4 872
一个人的身影
一个人的身影 2020-11-22 05:13

There is two Forms in my project : Form1 and Form2. There is a button in Form1, and what I want to do is closing Form1 and showing Form2 when that button clicked.

Fi

4条回答
  •  無奈伤痛
    2020-11-22 05:18

    Write that into your method which is executed while FormClosing event occure.

    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
          // Display a MsgBox asking the user if he is sure to close
          if(MessageBox.Show("Are you sure you want to close?", "My Application", MessageBoxButtons.YesNo) 
             == DialogResult.Yes)
          {
             // Cancel the Closing event from closing the form.
             e.Cancel = false;
             // e.Cancel = true would close the window
          }
    }
    

提交回复
热议问题