Asking for confirmation when “X” button is clicked

后端 未结 5 1657
时光说笑
时光说笑 2021-01-01 23:50

The problem is that the messagebox with \"sure you wanna close?\" does pop up, but when I click \"no\", it still proceeds to close the program. Any suggestions? Here\'s my c

5条回答
  •  遥遥无期
    2021-01-02 00:31

    e.Cancel on the FormClosing event will stop the closing process

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (CloseCancel()==false)
            {
                e.Cancel = true;
            };
        }
    
        public static bool CloseCancel()
        {
            const string message = "Are you sure that you would like to cancel the installer?";
            const string caption = "Cancel Installer";
            var result = MessageBox.Show(message, caption,
                                         MessageBoxButtons.YesNo,
                                         MessageBoxIcon.Question);
    
            if (result == DialogResult.Yes)
                return true;
            else
                return false;
        }
    

提交回复
热议问题