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 8420757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:56:40+00:00 2026-06-10T02:56:40+00:00

I am working on a C#-based utility that makes use of the FormClosing event,

  • 0

I am working on a C#-based utility that makes use of the FormClosing event, and the event is supposed to do different things depending on whether the form was closed programatically through the form.Close(); method, or by anything else (user clicking the X, program exiting, etc.)

The FormClosingEventArgs in the FormClosing event have a property called CloseReason (of enum type CloseReason).

CloseReason could be: None, WindowShutDown, MdiFormClosing, UserClosing, TaskManagerClosing, FormOwnerClosing, ApplicationExitCall.

Ideally, there would be a way to distinguish between when the user clicks the red X, and when the Close(); method is called (through the clicking of a Continue button after other actions are performed). However, the CloseReason property in FormClosingEventArgs is set to UserClosing in both cases, so there is no way to distinguish between when the user closes the form intentially, and when the form is programmatically closed. This goes contrary to my expectation that CloseReason would equal None if the Close() method is invoked arbitrarily.

    //GuideSlideReturning is an cancelable event that gets fired whenever the current "slide"-form does something to finish, be it the user clicking the Continue button or the user clicking the red X to close the window. GuideSlideReturningEventArgs contains a Result field of type GuideSlideResult, that indicates what finalizing action was performed (e.g. continue, window-close)

    private void continueButton_Click(object sender, EventArgs e)
    { //handles click of Continue button
        GuideSlideReturningEventArgs eventArgs = new GuideSlideReturningEventArgs(GuideSlideResult.Continue);
        GuideSlideReturning(this, eventArgs);
        if (!eventArgs.Cancel)
            this.Close();
    }

    private void SingleFileSelectForm_FormClosing(object sender, FormClosingEventArgs e)
    { //handles FormClosing event
        if (e.CloseReason == CloseReason.None)
            return;
        GuideSlideReturningEventArgs eventArgs = new GuideSlideReturningEventArgs(GuideSlideResult.Cancel);
        GuideSlideReturning(this, eventArgs);
        e.Cancel = eventArgs.Cancel;
    }

The issue with this is that when the Close(); method is invoked after the event GuideSlideReturning finishes without being canceled, the FormClosing event handler is unable to tell that the form was closed through the method instead of being closed by the user.

What would be ideal is if I could define what the FormClosing event’s FormClosingEventArgs CloseReason would be, like this:

    this.Close(CloseReason.None);

Is there a way to do this? The form.Close(); method does not have any overloads that accept any parameters, so is there a variable I can set or an alternate method I can call?

  • 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-10T02:56:41+00:00Added an answer on June 10, 2026 at 2:56 am

    Set a flag before calling close programmatically. This can be wrapped up in a private method:

    private bool _programmaticClose;
    
    // Call this instead of calling Close()
    private void ShutDown()
    {
        _programmaticClose = true;
        Close();
    }  
    
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing();
        _programmaticClose = false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I am working on a Linux based command line utility, that has to
I'm working with SOAP based web services that require authentication. I usually use the
I'm working on a Qt based software that involves some cryptography. The only three
I'm working on a larger Web based Project, that probably will have to handle
I have implemented some utility classes in Flex that I want to use in
I am working on a C# based application in which I have a utility
I'm currently working on a small utility program that only requires a command line
I'm working on utility method that allows conversion of XML data into formatted String
I am working on Lan based interoffice messaging system. The steps in my applications
i am working on voxel based game engine, in which i need to have

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.