Search code examples
c#.netwinformseventsformclosing

FormClosing event cancel from within another event?


For a school project, I have been asked to create a web browser. In the context menu I have an "Exit" button, which does a bunch of stuff like uploading data to an SQL database etc. If there is an error in the "try" block, the catch block offers the user a question if he wants to close or not.

In the "formclosing" event, I have connected my already existing "exit" functionality from the "exit" button, so that if the user presses the red "X" (on the window screen) to close the form it will have the same effect.

The problem is that because it's a "formclosing" event, it closes the form even if the user chose "no" in the question dialogue from the "Exit" button.

So my question is how do I cancel the "formclosing" request from within my "Exit" button?

P.S. Please ignore strange things like the delete sql command, our teacher has some strange requests. Assume that that is indeed the requested functionality.

private void יציאהToolStripMenuItem_Click(object sender, EventArgs e)
{
    Favorite fav;
    SqlCommand delfav = new SqlCommand("DELETE FROM tblFavorites", conn);
    SqlCommand addfav = new SqlCommand("INSERT INTO tblFavorites VALUES (@title, @url)", conn);
    addfav.Parameters.Add("@title", SqlDbType.NVarChar, 100);
    addfav.Parameters.Add("@url", SqlDbType.NVarChar, 300);
    try
    {
        conn.Open();
        delfav.ExecuteNonQuery();
        for (int i=0; i<favorites.Items.Count; i++)
        {
            fav = (Favorite)favorites.Items[i];
            addfav.Parameters["@title"].Value = fav.getsettitle;
            addfav.Parameters["@url"].Value = fav.getseturl;
            addfav.ExecuteNonQuery();
        }
        Application.Exit();
    }
    catch
    {
        DialogResult dialogResult = MessageBox.Show("There was a problem loading your favorites to the database. Do you still wish to quit? \n (All changes to the favorites list will be lost)", "", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            Application.Exit();
        }
        else
        {
            //Cancel the formclosing event here!
        }
    }
    finally
    {
        conn.Close();
    }
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    יציאהToolStripMenuItem_Click(null, null);
}

Solution

  • You should replace the functionalities for these two event handler. Also you should use this.Close() instead of Application.Exit()

    private void ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        // your code
        try
        {
            // your code
        }
        catch
        {
            DialogResult dialogResult = MessageBox.Show("There was a problem loading your favorites to the database. Do you still wish to quit? \n (All changes to the favorites list will be lost)", "", MessageBoxButtons.YesNo);
            if (dialogResult != DialogResult.Yes)
            {
                //Cancel the formclosing event here!
                e.Cancel = true;
            }
        }
        finally
        {
            conn.Close();
        }
    }