cancel event
-
I have a windows form with a button that I have set the DialogResult property. The button also has a click event. When the click event fires, my event methods displays an MessageBox with Yes or No buttons. (an 'are you sure' type message) If the user clicks the No button I want to cancel closing of the window form event. How I can I achieve this. Thanks
-
I have a windows form with a button that I have set the DialogResult property. The button also has a click event. When the click event fires, my event methods displays an MessageBox with Yes or No buttons. (an 'are you sure' type message) If the user clicks the No button I want to cancel closing of the window form event. How I can I achieve this. Thanks
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to close this window?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
this.Close();
}
//if anything other than Yes is clicked (like No), then nothing will happen.
} -
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to close this window?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
this.Close();
}
//if anything other than Yes is clicked (like No), then nothing will happen.
}The problem stems from button1 having it's DialogResult property set. When button1 click event is fired, it executes button1_Click, displays the are you sure dialog and regardless of the result of the are you sure dialog will close the window form because it has a DialogResult set. I was wondering if I could cancel this close window event depending on the result of the are you sure dialog. I can simply not set the DialogResult property of button1, and set the DialogResult in the button1_Click event depending on the result of the are you sure dialog. Regards.
-
The problem stems from button1 having it's DialogResult property set. When button1 click event is fired, it executes button1_Click, displays the are you sure dialog and regardless of the result of the are you sure dialog will close the window form because it has a DialogResult set. I was wondering if I could cancel this close window event depending on the result of the are you sure dialog. I can simply not set the DialogResult property of button1, and set the DialogResult in the button1_Click event depending on the result of the are you sure dialog. Regards.
Why not remove the DialogResult from button1 and assign it to the window only if it is closing:
private void button1_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to close this window?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
this.DialogResult = DialogResult.Cancel; //or whatever
this.Close();
}
//if anything other than Yes is clicked (like No), then nothing will happen.
}