I have a form that, among other controls, has a TextBox field that can be optionally populated by the user.
There is also a btnSubmit button that performs the necessary actions and closes the form.
In my code, I check if a comment is present, and if not, ask the user if he wants to fill it out before exiting.
I use the bool function AskToFillCommentIfNeeded () , which shows that the MessageBox asks the user to optionally fill out a comment before exiting, if it has not already done so.
It returns true if the user replied Yes, otherwise false.
If the user clicks “Yes”, I must exit the submit function without closing the form so that the user can enter a comment and then click the submit button again.
The code [edit] looks something like this:
private void btnSubmit_Click(object sender, EventArgs e)
{
if (AskToFillCommentIfNeeded()) { return; };
}
I use this code in a non-modal form and works as expected.
But when I try to use it in a modal form with the btnSubmit DialogResult attribute set to OK, it does not work as expected:
instead of just exiting the event receiver, it closes the form without saving data.
I created a (clumsy) workaround using a boolean flag like this:
private bool isBusy = false;
private void btnSubmit_Click(object sender, EventArgs e)
{
isBusy = true;
if (AskToFillCommentIfNeeded()) { return; };
isBusy = false;
}
Thus, if the return statement is executed, I intercept the _FormClosing event and cancel it if the flag is set to true:
private void FDialog_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = isBusy;
}
, .
?
# Express 2010.
.
jack griffin