I have a MessageBox attached to the closing event of my form checking that the user wants to close the form.
When the event is triggered, the MessageBox should display, asking the user to select 'Yes' or 'No'. Instead, the MessageBox line is run but it is not displayed and the DialogResult is automatically set to 'No' with no user interaction.
I tried actively setting the DialogResult to 'Yes' beforehand and it still got set to 'No' with no user interaction, even though the MessageBoxDefaultButton is set to 'Yes' (Button1).
Can anybody identify what could be causing the MessageBox to be skipped?
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
closeForm(e);
}
private void closeForm(FormClosingEventArgs e)
{
DialogResult exityesno = MessageBox.Show("Are you sure you want to exit?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
if (exityesno == DialogResult.Yes)
{
// close form
}
else if (exityesno == DialogResult.No)
{
e.Cancel = true;
}
}