#1
  1. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27

    Stuck in an infinite loop


    I have a WinForm with some TextBoxes. When they're changed a member variable bDirty is set to true so that I know that something has changed and a save should be required. So in the FormClosing event I check for bDirty. If true, I ask the user to save with a YesNoCancel MessageBox. If they choose no (do not save) I call this.Close(), which just fires the same event and same function again! So how do I actually close this form?
    Code:
    private void League_FormClosing(object sender, FormClosingEventArgs e)
            {
                // check for dirty form and take appropriate action
                if (m_bDirty)
                {
                    DialogResult result = MessageBox.Show("Would you like to save your changes?", 
                        "Warning", 
                        MessageBoxButtons.YesNoCancel, 
                        MessageBoxIcon.Warning);
    
                    if (result == DialogResult.Yes)
                    {
                        SaveForm();
                    }
                    else if (result == DialogResult.No)
                    {
                        this.Close();
                    }
                    else if (result == DialogResult.Cancel)
                    {
                        e.Cancel = true;
                    }
                }
            }
    The FormClosingEventArgs has a CloseReason property but I can't seem to find out how/if that will help.

    Thanks!
    Discontent is the first necessity of progress. - Edison
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,204
    Rep Power
    2012
    It has already been asked to close the form, so what if you leave the no option empty? i.e. comment out this.Close().

    Comments on this post

    • mateoc15 agrees
    • eclipsed4utoo agrees : agreed
  4. #3
  5. Business Analyst
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2004
    Location
    The 'Ville
    Posts
    1,087
    Rep Power
    27
    Duh... will try, that pretty much has to work!
    Discontent is the first necessity of progress. - Edison

IMN logo majestic logo threadwatch logo seochat tools logo