Ravinder Gangadher
Ravinder Gangadher

Reputation: 1016

How to close entire application when application has more than 2 forms without showing messagebox more than once

I am developing a application using windows forms. The project contains 3 forms: one login form which is the main form and two others which are child forms to the login form.

My problem is when want to close the total application by using Application.Exit() in form closing event my messagebox showing the dialog more than once.

1.This code in Login form i.e main form:

private void FrmLogIn_FormClosing(object sender, FormClosingEventArgs e)
    {
        DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
        if (loginResult == DialogResult.Yes)
        {
            Application.Exit();
        }
    }

2.AdminForm closing event which is child form to login form:

 private void FrmAdmin_FormClosing(object sender, FormClosingEventArgs e)
    {
        DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
        if (loginResult == DialogResult.Yes)
        {
            Application.Exit();
        }
    }

3.Billoperations form closing event which is child form to login form:

private void FrmBillOperation_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult loginResult = MessageBox.Show("Do you want to close this application?","Close",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
    if (loginResult == DialogResult.Yes)
    {
        Application.Exit();
    }
}

When i click the close button in any form it will show MessageBox message only once. Please help me.

Upvotes: 1

Views: 5050

Answers (4)

user1943915
user1943915

Reputation: 133

private void sh_interface_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
          foreach (Form f in Application.OpenForms)
            {
                if (!f.IsDisposed)
                f.Dispose();
            }
        }
        else
        {
            e.Cancel = true;
            this.Activate();
        }   
    }

This will close all forms including the hidden forms and the main form from Application.Run(new something())...Also this method works when invoked in inherited classes while coded in template class Form Closing event.

Upvotes: 0

stuartd
stuartd

Reputation: 73293

The FormClosingEventArgs instance passed to the FormClosing event has a CloseReason property, which will be set to CloseReason.ApplicationExit when the Exit method of the Application class has been invoked: your handlers should check for this condition and if so then take no further action.

private void FrmLogIn_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.ApplicationExit)
        return;

   ...
}

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can try with this code

FormCollection fc = Application.OpenForms;
if (fc!= null && fc.Count > 0)
{
   for (int i = 1; i < fc.Count; i++)
    {
       if (fc!= null && fc.IsDisposed!= true)
        {
          fc.Dispose();
        }
   }
}

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151674

Make all FormClosing methods call a ApplicationShutdown function which handles this in a central place. You don't want to copy this code to every new form you create.

In this method you can check a boolean (watch for thread-safety) called for example IsShuttingDown. If it's already true, leave the method, otherwise you ask the question and start exiting.

Upvotes: 1

Related Questions