none
How to exit the entire application on click of the close button on the MDI Form?

    Question

  •  

    Hi Sir,

    I have a  windows application c#.net2005 which contails an MDI Form and many other forms.

    When I click the close buttonof the MDI Form, only the MDI Form is closed  and the application is still running.

    I want to exit the complete application, when I click the close button of the MDI Form.

    Please help me to achieve the functionality with the code snippet.

    Please also mention on which event handler I must write the code.

    Thanks and Regards,

    JM

    Friday, January 11, 2008 6:44 AM

Answers

  • Okey do like this.. in case of using Application.Exit(); User Application.ExitThred(); it will close all the windows on the current thread.  

    private void frmMDI_FormClosing(object sender, FormClosingEventArgs e)

            {

                    if (MessageBox.Show("Are sure to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)

                    {

                        e.Cancel = true;

                    }

    else

    {

       Application.ExitThread();

    }

              

            }





    Tuesday, January 15, 2008 9:28 AM

All replies

  • You can use Application.Exit();  to exit the application.
    • Proposed as answer by Nithiyanantham Tuesday, August 16, 2011 2:25 PM
    Friday, January 11, 2008 7:01 AM
  • Hi Sir,

    Please also explain on which event handler i have to write the code Application.Exit(); 

    on click of the close button of the MDi Form. That is where I am confused.

    Thanks and Regards,

    JM

    Friday, January 11, 2008 7:14 AM
  • Vayalumkal...

    Any event you can write this.. If you have a menu item called close application then you can write this on it's click event.
    Friday, January 11, 2008 7:17 AM
  • I have a munu item called Exit.

    I wrote the code given by you on click of that and it worked successfully for me.

    Now I want toexecute the code by clicking the close button on the top right hand side of the MDI Form.

    What I dont know is what is the event handler for the close button on the top right hand side of the MDI Form.

    Please help me with the code snippet.

    Lot of thanks and Regards,

    -jm

    Friday, January 11, 2008 9:06 AM
  • donot need to write code to close the application on clicking on the close button. Because it is automatically closes the application. If you want to stop closing application while clicking on the close button on the right hand side of the mdi form. then you  can do like this.


    Code Block

    private void frmMdi_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (MessageBox.Show("Are sure to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                }

            }

    Friday, January 11, 2008 9:17 AM
  • Hello,

     

    Did you run any foreground thread before you close the form?

     

    Any running foreground thread will prevent the process from being terminated.

    So it will still run in the background even if you have closed the form.

     

    Friday, January 11, 2008 10:33 AM
  • Use Application.Exit() on the MDI Form FormClosed event.

    Friday, January 11, 2008 10:50 AM
  • Hi,

    Sir I just reproduced the code given by you as follows on the  FormClosing event.

     

    private void frmMdi_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (MessageBox.Show("Are sure to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                }

            }

    Everything is working fine. I could exit from the application.

    But it is not showing the Message Box. Without that the application isgetting exited.

     

    Instead, when i click onthe close button, it should show me the message box with buttons Yes and No for Conformation.

    When I click the Yes button, I should exit from the Application.

     The present problem is that it is not displaying the message box for confirmation.

    I hope you understood the senario.

    Please help me by giving the code snippet.

    Thanks and Regards,

    -jm

     

     

    Friday, January 11, 2008 12:42 PM
  • You have missed the code for attaching the function with event handler...

    You can do one thing..  add the below code in the form's load method...

    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMdi_FormClosing);
    Monday, January 14, 2008 7:02 AM
  • Hi Sir,

    Very sorry for the delay

    I have a windows application c#.net 2005 which contains an MDI Form and many other forms.

    I want to exit the complete application, when I click the close button of the MDI Form.

    Please help me to achieve the functionality with the code snippet.

    Please also mention on which event handler I must write the code.

    I am not running any foreground thread before I close the form.

    I have tried with the following code as directed by you.

     

    private void frmMDI_FormClosing(object sender, FormClosingEventArgs e)

            {

                if (MessageBox.Show("Are sure to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)

                {

                    e.Cancel = true;

                }

            }

     

     

     

    private void frmMDI_Load(object sender, EventArgs e)

            {

                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMDI_FormClosing );

            }

     

     

    When I click on the close button, it should show me the message box with buttons Yes and No for Conformation.

    When I click the Yes button, I should exit from the Application.

    When I click the No button, the form should remain like that.

     The present problem is that it is not at all displaying the message box for confirmation.

    I hope you understood the scenario.

    Please help me by giving the code snippet.

     

    Thanks and Regards,

    JM

     

     

    Monday, January 14, 2008 8:45 AM
  • I donot found any error on your code.. you have written exactly what needed.. You can do one thing , just put the break point on the frmMdi_FormClosing event and check whether this event is arising while closing the mdi form..
    Monday, January 14, 2008 9:43 AM
  • I think what is happening here is that you are not correctly listening to the events, my guess is that you didn't subscribe the Form_Load method to the Form.Load event.

    Put the code inside the Form_Load method inside the constructor instead:

     

    Code Block

    private void frmMDI()

    {

    InitializeComponent();

    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMDI_FormClosing );

    }

     

     

    See if this will make it work.

    Regards,

    Fábio

    Monday, January 14, 2008 10:53 AM
  • Yes that may be the problem.. it is better you debug your code so that you can understand whether it is reaching the code you written or not.
    Monday, January 14, 2008 10:56 AM
  • are you sure you give your childform your MdiParent???

     

    childform1.MdiParent = this.MdiParent;

     

    Monday, January 14, 2008 11:38 AM
  • Hi,please check following code

    Code Block

    private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {

        if (MessageBox.Show("Do you want to exict?", "My Application", MessageBoxButtons.YesNo) == Windows.Forms.DialogResult.Yes) {

            Application.Exit();
        }
        else {
            e.Cancel = true;
        }

    }

     

    You should use Application.Exit();

    If you have any further questions ,feel free to let me know.

     

    Best Regards

     

    Gavin

     

     

    Tuesday, January 15, 2008 6:03 AM
  • Hi Sir,

    Actually there are two forms.

    One is Login Form and the other is MDI Form.

    Login form is outside the MDI Form.

     

    The complete senario is clearly explained below.

     

     

     

    Hi Sir,

    Sorry for the delay

    I was trying with the code given by you. It is working if the project contains an MDI Form only and I have commented the following lines of code

     

     

     

     

      public frmMDI()

            {

                InitializeComponent();

                //this.Load += frmMDI_Load;

                //this.FormClosing += frmMDI_FormClosing;

            }

    If I uncomment the above lines and if I click the Close button of my MDI Form, a message box will appear. But the problem is, if I am clicking the No button on the message box, I have to click two times. Then only the message box will disappear.

    Similarly I have to click the Yes button Two times. Then only the application will be exited. But when I uncommented the above two lines of code, it is working fine for me.

    The above scenario is working fine only when the application has an MDI Form and no other Forms.

     

    But in my project I have a Login Form and an MDI Form.

    After entering the user name and password in the respective textboxes, I will click the Login Button. Then it will take me to the MDI Form

    In the Login Form I have written the following code.

     

     

     

    private void btnLogin_Click(object sender, EventArgs e)

            {

                SqlConnection objConn = new SqlConnection();

                objConn.ConnectionString = "server=localhost;user=sa;integrated security=true;database=MICO";

                objConn.Open();

     

                SqlCommand objCmd = new SqlCommand();

                objCmd.CommandText = "select UserID,Password from vew_UserDetails where UserID='" + textBox1.Text + "' and Password='" + textBox2.Text + "' ";

                objCmd.Connection = objConn;

     

                SqlDataReader objRdr;

                objRdr = objCmd.ExecuteReader();

                if (objRdr.Read())

                {

     

                    //this.Hide();

                    frmMDI objMDI = new frmMDI();

                    objMDI.Show();

                }

     

                if (objConn != null) objConn.Close();

            }

    If the user name and password(fetching from the database)are correct, It will show the MDI Form.

     

     

    In the MDI Form  code behind, I have written the following codes in the respective places.

     

     

     

     

     

     

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;
    using System.Runtime.InteropServices;
    using System.Drawing

     

     

     

     

    public frmMDI()

            {

                InitializeComponent();

                //this.Load += frmMDI_Load;

                //this.FormClosing += frmMDI_FormClosing;

            }

            private bool bClosing;

     

     

     

    private void frmMDI_FormClosing(object sender, FormClosingEventArgs e)

            {

                if (!bClosing)

                {

                    bClosing = true;

     

                    if (MessageBox.Show("Are sure to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)

                    {

                        e.Cancel = true;

                    }

                }

                else

                {

                    bClosing = false;

                   

                }

     

            }

     

     

    private void frmMDI_Load(object sender, EventArgs e)

            {

                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMDI_FormClosing);

     

            }

     

     

     

    Here when I click the close button of the MDI Form, the message box will appear with Yes, No buttons.

    If I click the No button, it will disappear. (That is fine)

    But if I click the Yes button, the MDI Form will be closed. But the application will still be running. I have to manually stop.

     

    I am sure I got 99 % solution.

    But I request you to give me the complete solution to tackle this.

    I have to include this feature in my small project. I it would be very much grateful if you could give me the complete solution.

    Thanks and Regards

     

    Tuesday, January 15, 2008 8:56 AM
  • Okey do like this.. in case of using Application.Exit(); User Application.ExitThred(); it will close all the windows on the current thread.  

    private void frmMDI_FormClosing(object sender, FormClosingEventArgs e)

            {

                    if (MessageBox.Show("Are sure to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)

                    {

                        e.Cancel = true;

                    }

    else

    {

       Application.ExitThread();

    }

              

            }





    Tuesday, January 15, 2008 9:28 AM
  • Sorry to miss on thing.. Please remove below line code from your form_load event..

    this
    .FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMDI_FormClosing);



    private void frmMDI_Load(object sender, EventArgs e)

            {

    //Remove this line below

                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMDI_FormClosing);

     

            }


    Once check the frmMDI.Designer.cs file.. for the line


    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMdI_FormClosing);



    in the InitializeCompnent() method... If it is not there then you need the code to be either written there or in form_load method..


    Tuesday, January 15, 2008 9:37 AM