Song Lee

Song Lee

  • NA
  • 47
  • 37.4k

Winform Alt F4 exit YesNo dialog

Nov 6 2014 7:18 PM
 Hello, I want to add in a feature.
When exit button is clicked, it'll give Yes or No option.
If Yes, application closes, if No, nothing happens. 
I have no problem with the coding below when it comes to "Click",
but when I press "Alt F4", program closes even if I click "No"
 
 
private void exitButton_Click(object sender, EventArgs e)
{
    while(MessageBox.Show("Exit app?","Exit message",MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
       this.Close();
     }
       //If "No", MessageBox closes
}
 
//////////////////////////////////////////////////////////////////////////////////
 
private void MainForm_KeyDown(object sender, KeyEventArgs e)
    if (e.Alt && e.KeyCode == Keys.F4)
    {
       exitButton_Click(null, null);
     }
 |

Answers (2)