How to exit a function without exiting a form when the form is modal?

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)
{
     // ask user if he wants to fill the comment : if so, exit this function
     if (AskToFillCommentIfNeeded()) { return; };

     // ... save data and exit form ...
}

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)
{
     // clumsy attempt to avoid form exit :
     isBusy = true;

     // ask user if he wants to fill the comment : if so, exit this function
     if (AskToFillCommentIfNeeded()) { return; };

     isBusy = false;

      // ... save data and exit form ...
}

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)
{
     // abort closing if flag is set
     e.Cancel = isBusy;
}

, .
?
# Express 2010.
.

jack griffin

+3
3

DialogResult None, :

private void btnSubmit_Click(object sender, EventArgs e)
{
     if (AskToFillCommentIfNeeded()) {
         this.DialogResult = DialogResult.None; 
         return;
     }

     // ... save data and exit form ...
}
+3

button.DialogResult = None form.DialogResult button.Click .

+1

: ,
8 , .
, , , .
( , :-)).

, ! . @Hans Passant: , .
@mrtofigh: .DialogResult , form.DialogResult.
@Henk Holterman: .
, , , -.
, :

private void FDialog_FormClosing(object sender, FormClosingEventArgs e)
{
    // intercept form closing from Close box in title bar
    if (e.CloseReason == CloseReason.UserClosing)
        {
             e.Cancel = !ConfirmFormExit();
        }
}

ConfirmFormExit() - bool, , MessageBox , true Yes.

, :

private void btnSubmit_Click(object sender, EventArgs e)
{
    //  avoid form exit right now
    this.DialogResult = DialogResult.None;

    // ask user if he wants to fill the comment : if so, exit this function 
    if (AskToFillCommentIfNeeded()) { return; };

    // now is safe to return dialog result
    this.DialogResult = DialogResult.OK;

    // ... save data and exit form       
}

, .
.

0
source

All Articles