Yukimoto Otomikuy Yukimoto Otomikuy - 3 years ago 103
C# Question

MessageBoxButton.YesNo Tutorial

I have a little problem again but this time it regards the

MessageBoxButton.YesNo
. This is my problem: I don't know what's the right syntax so that if I hit Enter(keyboard) or click Yes, the
Application.Exit();
will execute and if I hit Esc(keyboard) or click No, the
Application.Exit();
will not execute. This is my code:

MessageBox.Show("Are you sure you want to exit?","Application Exit", MessageBoxButtons.YesNo);
Application.Exit();

Answer Source

You need to actually store off and check the result of the message box

var result = MessageBox.Show("Are you sure you want to exit?", "Application Exit", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
    Application.Exit();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download