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

  • Home
  • SEARCH
  • 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 6752049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:59:57+00:00 2026-05-26T12:59:57+00:00

I am currently developing a C# Windows Form Application. After the user has login

  • 0

I am currently developing a C# Windows Form Application.

After the user has login through the loginForm, it will be brought to the mainForm.

I would like to code it in a way that after the user click the cross on the title bar in the mainForm, there would be a prompt asking the user “This will close the application. Confirm?”
followed by a yes and no button.

If yes, another box will be displayed, “Application Closed!”

If no, the messagebox will just close and the application will continue running.

My current code is :

private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
        System.Windows.Forms.Application.Exit();
    }
    else
    {
        this.Activate();
    }   
}

however it does not work.

Firstly I have no idea why the messagebox pops up twice in order for the whole thing to close.

Secondly if i click no, the form closes as well and I am not able to bring it back.

  • 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-05-26T12:59:58+00:00Added an answer on May 26, 2026 at 12:59 pm

    To cancel the closing of the form, in your else statement you need e.Cancel = true;.
    You don’t need the explicit Exit in your true case.

    Give this a try

    private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
    
        }
        else
        {
            e.Cancel = true;
            this.Activate();
        }   
    }
    

    I assume your second MessageBox to say it has closed if for testing purposes only.

    You probably only want.

    private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) != DialogResult.Yes)
        {
            e.Cancel = true;
        } 
    }
    

    Notice the statement checks to see if they didn’t hit yes, rather than if they hit cancel. This means that if they hit the x on the dialog box it won’t be counted as a confirmation.

    EDIT: If mainForm isn’t the main form

    Okay, I think I’ve got what you’re asking now.

    What I would do is put the code I have in my second code block above in the FormClosing, and then in the FormClosed event handler have this

    private void mainForm_FormClosed(Object sender, FormClosedEventArgs e)
    {
        MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
        System.Windows.Forms.Application.Exit();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I am developing a windows form application in c# that has several forms.
I'm currently developing a application where I will have multiple windows open using C#
I am currently developing a C# Windows Form Application. Now I am trying to
I am currently developing a C# Windows Form Application. In the main form, there
I am currently developing a C# Windows Form Application that I intend to let
I am currently developing a C# Windows Form Application that I intend to let
I am currently developing a C# Windows Form Application that I intend to let
I am currently developing a C# Windows Form Application that I intend to let
I am currently developing a C# Windows Form Application that I intend to let
I am currently developing a C# Windows Form Application that I intend to let

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.