Asking for confirmation when “X” button is clicked

后端 未结 5 1645
时光说笑
时光说笑 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:25

    You are expected to set the Cancel property of the FormClosingEventArgs argument to true when you require the close-operation to be cancelled. And an explicit Environment.Exit(0) is normally not required since the form is on its way to being closed any way (the cancellation of the shutdown process is opt-in, not opt-out).

    Replace the last bit with:

    var result = MessageBox.Show(message, caption,
                                 MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question);
    
    e.Cancel = (result == DialogResult.No);
    

提交回复
热议问题