Asking for confirmation when “X” button is clicked

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

    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
    

提交回复
热议问题