Form Close button events, confirm the exit, do not quit canceled

Click the close button upper right corner of the form when the confirmation pop-up window, click OK to exit, click Cancel, Cancel to exit, pay attention, do not confirm the lower write this.Close (); so there will be an infinite loop and can not exit the form

Example code:

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MsgBox.ShowQuestion("退出系统吗?") == DialogResult.OK)
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
            }
        }


Reproduced in: https: //my.oschina.net/dongri/blog/610899

Guess you like

Origin blog.csdn.net/weixin_33748818/article/details/91765882