Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:07:36+00:00 2026-06-12T14:07:36+00:00

When exiting the program if the programmer handles the FormClosing event if there is

  • 0

When exiting the program if the programmer handles the FormClosing event if there is unsaved data the programmer can prompt a save dialog asking them if they want to save, not save, or cancel the operation.

In the FormClosing event if they hit Cancel, to cancel the operation you set e.Cancel = true;. This is fine. However, in the menu if the user hits the Exit button then the same thing should occur except instead of doing e.Cancel = true; it should just do return; because it’s handle menu item Click event instead of a FormClosing event.

Right now I pretty much have the code duplicated and was wondering if there was a way to handle this to avoid the duplication in an elegant and understandable fashion.

private void FrmEditorFormClosing(object sender, FormClosingEventArgs e)
{
    if (NeedsToSave)
    {
        DialogResult saveChangesDialog = 
MessageBox.Show("There are unsaved changes. Save now?", 
"Xml Editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

        if (saveChangesDialog == DialogResult.Yes)
        {
            Save();
        }
        else if (saveChangesDialog == DialogResult.Cancel)
        {
            e.Cancel = true; // Only difference
        }
    }
}

private void ExitToolStripMenuItemClick(object sender, EventArgs e)
{
    if (NeedsToSave)
    {
        DialogResult saveChangesDialog = 
MessageBox.Show("There are unsaved changes. Save now?", 
"Xml Editor", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

        if (saveChangesDialog == DialogResult.Yes)
        {
            Save();
        }
        else if (saveChangesDialog == DialogResult.Cancel)
        {
            return; // Only difference
        }
    }

    Close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:07:37+00:00Added an answer on June 12, 2026 at 2:07 pm

    You shouldn’t need to do this. Calling Close in your menu click handler should cause the FormClosing event to fire and you can cancel it there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to save the context of the program before exiting ... I've put
I was wondering if there is a library for editing program flow. I refer
Aspiring programmer here! You can skip this part if you find it irrelevant. Im
Why does this print garbage instead of exiting my program gracefully? I use system
What is the best-practice for gracefully exiting a Node.js program from an external signal?
Possible Duplicate: Is freeing allocated memory needed when exiting a program in C I
Hi there I am looking for common mistakes pl/sql programmers make when they program
Are there any good Android apps - text editors, optimised for programmers? I'm asking
I have an existing program we've developed as a large set of Eclipse plugins.
I'm converting an existing program to C++ and here need to manipulate Sybase timestamps.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.