Asking for confirmation when “X” button is clicked

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

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 code:

    protected override void OnFormClosing(FormClosingEventArgs e)     {         CloseCancel();     }      public static void 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)             Environment.Exit(0);     } 

回答1:

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); 


回答2:

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;     } 


回答3:

just take it simple

protected override void OnFormClosing(FormClosingEventArgs e)         {                         base.OnFormClosing(e);             if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)             {                 Dispose(true);                 Application.Exit();             }             else             {                 e.Cancel = true;             }         }          private DialogResult PreClosingConfirmation()         {             DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);             return res;         } 


回答4:

protected Boolean CanClose(Boolean CanIt) {    if(MessageBox.Show("Wanna close?", "Cancel Installer", MessageBoxButtons.YesNo,       MessageBoxIcon.Question).ShowDialog() == DialogResult.Yes)    {       // Yes, they want to close.       CanIt = true;    }    else    {      // No, they don't want to close.      CanIt = false;    }     return CanIt; }  protected override void OnFormClosing(FormClosingEventArgs e) {     if(CanClose(false) == true)     {        this.Dispose(true);     }     else     {        e.Cancel = true;     } } 

I'm unsure of how to handle the other scenario you mentioned (handling the 'X' click). Maybe you could do something like this (psuedo-code):

// IF CLICKED YES    THEN CLOSE // ELSE-IF CLICKED NO    THEN DO NOTHING // ELSE    THEN DO NOTHING 


回答5:

The question is now old but this way is more simple and short, and I think it can be useful to those who arrive on this page:

protected override void OnFormClosing(FormClosingEventArgs e) {     if (MessageBox.Show("Are you sure that you would like to cancel the installer?", "Cancel Installer", MessageBoxButtons.YesNo) == DialogResult.No)     {         e.Cancel = true;     } } 

and use elsewhere this.Close() rather than a function.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!