2
Answers

Winform Alt F4 exit YesNo dialog

Song Lee

Song Lee

10y
1.5k
1
 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)