C# 的隨手筆記 1 - Close Event 關閉視窗時 跑出MessageBox的提示

關閉視窗→Close Event →MessageBox Yes / No

關閉視窗→Close Event →MessageBox Yes / No

  1. Yes 關閉
  2. No 不關閉
        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(Close_Event_MessageBox);
        }


        private void Close_Event_MessageBox(object sender, CancelEventArgs e)
        {


            if (MessageBox.Show("是否要關閉程式?", "關閉程式", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {

                e.Cancel = false; 
            }
            else
            {

                e.Cancel = true;   
            }
        }