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#.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 snippet.

     

    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 necessary code snippet.

     

    Thanks and Regards,

    JM

     

    Monday, January 14, 2008 8:48 AM

Answers

  • First, even if the login is not visible it is still possible to read the values of the textboxes on the login form, so after you perform the login, you should clean up the password textbox AND you should use the password character property.

    So when you perform the login you should do something like this:

     

    txtPassword.Clear();

     

    and to make the login form invisible, after the login you use:

     

    this.Visible = false;

     

    Another thing, I've seen your posts lately and I can see you are REALLY inexperienced in programming. I strongly encourage you to look for tutorials over the internet or buy yourself a book and get some self-training. Here is not a very good place to learn, it is a good place to clarify specific doubts. Programming is not so simple for begginners so you really should get some training before trying to learn something new here.

     

    Regards,

    Fábio

    Tuesday, January 15, 2008 12:12 PM

All replies

  • Hi Vayalumkal,

    Ensure that You have subscribed for form's Load event successfully.
    Next code snippet worked well for me:

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

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                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 );
            }
        }
    }

    Hope this helps.
    Regards,
    Oleh.
    Monday, January 14, 2008 3:48 PM
  • Hi Oleh,

     

     

    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 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,

    -jm


     

    Tuesday, January 15, 2008 8:57 AM
  • Hi Vayalumkal,

    OK. I did not know that it was not application's main form.
    Call Application.Exit method explicitly.

    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

     {

         Application.Exit();

     }

                }

                else

                {

                    bClosing = false;

                   

                }

     

            }



    Regards,
    Oleh
    Tuesday, January 15, 2008 9:12 AM
  • Hi Sir,

    Still 0.5 % not working.

    Once again be patient and please go through the following code snippet.

    I have written the following code snippet in the respective places of the code behind of MDI Form.

     

     

    using System;

    using System.Collections.Generic;

    using System.Windows.Forms;

    using System.Threading;

    using System.Runtime.InteropServices;

    using System.Drawing;

     

     

     

    public frmMDI()

    {

    InitializeComponent();

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

    }

     

    private bool bClosing;

     

    private void frmMDI_Load(object sender, EventArgs e)

    {

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

    }

     

     

    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

    {

    Application.Exit();

    }

    }

    else

    {

    bClosing = false;

    }

    }

     

     

    Please notre that I have commented the code inside  frmMDI_Load Event Handler.

    Everything is working fine.

    When I click the close button of the MDI Form, message box will appear.

    If I click the No button on that(only once), the message box will disappear.

    But I have to click the Yes button two times to get the application exited

     

     

    If I decommend the code inside the frmMDI_Load Event Handler, I have to click the No button also to get the Message Box to get disappeared.

    So I request  you to give  a solution for this also.

    Thanks and Regards,

    -minto

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    Tuesday, January 15, 2008 9:50 AM
  • Vayalumkal,

    Please update
    frmMDI_FormClosing method to:

            private void frmMDI_FormClosing( object sender, FormClosingEventArgs e )
            {
                if( !bClosing && !e.Cancel )
                {
                    bClosing = true;

                    if( MessageBox.Show( "Are sure to exit?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.No )
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        Application.Exit();
                    }
                }

                if( e.Cancel && bClosing )
                {
                    bClosing = false;
                }
            }

    Regards,
    Oleh.
    Tuesday, January 15, 2008 10:10 AM
  •  

    Hi Mr.Oleh Svintsitskyy,

     

    Thanks a lot for the efforts taken by you for me.

    It worked wonderfully for me.

     

    But still a minor problem exists.

     

    After logged in, if I minimize the MDI Form, I can see the Login Form in the Background.

    Since this may affect the security of the system( ie if  a third party user comes and minimizes the MDI Form , he can read the user ID and password entered in the textboxes, moreover I have not used the password character property of the textbox also  )

    So when the MDI Form is minimized, the Login Form should not be seen(visible).

    Please give me a right solution for this.

    Please help me with the complete code snippet.

    Thanks a lot and much Regards,

    -jm

    Tuesday, January 15, 2008 11:53 AM
  • First, even if the login is not visible it is still possible to read the values of the textboxes on the login form, so after you perform the login, you should clean up the password textbox AND you should use the password character property.

    So when you perform the login you should do something like this:

     

    txtPassword.Clear();

     

    and to make the login form invisible, after the login you use:

     

    this.Visible = false;

     

    Another thing, I've seen your posts lately and I can see you are REALLY inexperienced in programming. I strongly encourage you to look for tutorials over the internet or buy yourself a book and get some self-training. Here is not a very good place to learn, it is a good place to clarify specific doubts. Programming is not so simple for begginners so you really should get some training before trying to learn something new here.

     

    Regards,

    Fábio

    Tuesday, January 15, 2008 12:12 PM
  • Dear Mr. Fabio, 

    Every thing has worked fine with your

    txtPassword.Clear();

    and

    this.Visible = false;

    Please accept my congratulations.

    I got job in a small software firm and I was assigned a work which involved these tasks which I could successfully do with your valuable help. If I am to read a book to find out these things, I don’t think that I would have been able to resolve these issues within the time span given to me.

    I also thank you for your valuable suggestions.

    Anyway I have solved the present problem with your help only.

    I also request you to give me some Internet tutorials links or some web site urls which are simple and easy to understand for a beginner.

    Thanks and Regards,

    -jm

     

     

    Wednesday, January 16, 2008 4:35 AM
  • I understand you Vayalumkal and I guess I was not aware of your time span limitations. But I'm glad you got interested about learning stuff. I will give you links for both books and tutorials. I think books are better because they are almost always more complete then internet tutorials and you can have them on your hands but if you can't afford, don't like or don't have the patience to read books, internet tutorials are also valuable resource. For your case I think you need book/tutorials on two fronts. 1 - The C# Language introduction, 2 - Windows Forms Programming.

     

    - Books (if you live outside US, books get delivered worldwide through Amazon, that is what I do):

     

    --- C# Language:

    Learning C# (Little to no experience in programming)

    or

    Programming C# (Some programming experience (C++/Java/VB))

     

    --- Windows Forms

    Windows Forms 2.0 Programming

     

    - Internet Tutorials (Usually very superficial and topic specific):

     

    --- C# Language:

    MSDN C# Tutorials

     

    --- Windows Forms:

    Tutorial 1 (Older)

    Tutorial 2

     

    Anyways if you browse the internet you will find lots of other tutorials.

    Anything else fell free to ask.

     

    Regards,

    Fábio

    Wednesday, January 16, 2008 11:08 AM
  •  

    Dear Mr.Fábio

    My sincere thanks and gratitudes for this valuable inoformation.

    Again  if I come across with any problem in future, Please dont hesitate to help me.

    Thanks and Regards,

    -jm

    Wednesday, January 16, 2008 1:34 PM
  • Sir ,

    If you dont mind would you please answer the below written question also?

     

    I had posted this MSDN Forums, but nobody has answered.

     

    Question-How to validate a textbox for an email id?

     

    Sir,
    I have a windows application c#. Net2005, which contains a Form with one text box to enter an email id.
    I want to give validation for this textbox (ie) the email id should be in the correct format
    After typing the email id in the textbox and when you press the enter key, it should pope up a message box telling that “The Email Id you entered is not in the correct format” (if it is not in the correct format)


    It should also be taken care that the number of characters before the symbol @ should be between 4 and 30( >=4 and <=30)

    I have added the name space 

    using System.Text.RegularExpressions;

     

    Currently I have a program as follows

     

     

    private void button1_Click(object sender, EventArgs e)

     {

          // This would be what the  user has entered

                string emailAddress = textBox1.Text;

     

                // Create a group called username becuase we want to look at it further

                // and we are ignoring everything after the @ symbol

                const string pattern = "^(?<username>.+)@(.)+$";

     

                //Test the format and then store results in a Match object

                Match myMatch = Regex.Match(emailAddress, pattern);

                int cnt = myMatch.Groups["username"].Length;

     

                if (((cnt < 4) || (cnt > 30))|| !myMatch.Success ) // this is the range we want

                {

                   

                   

                    MessageBox.Show("Problem with MailID");

     

                    textBox1.SelectAll();

                }

     

                else

                {

                    MessageBox.Show("Mail ID  is in the correct format");

                }

      }

     

     

    Now this is working fine.

    But I want to give a small validation also.

    Ie if I give abcd@yahoo    it will show  Mail ID  is in the correct format

    But I want to give  a validation for a dot symbol and some 2-3 characters after the string yahoo

     

    For example abcd@yahoo.co  or com  ie 2-3

    If the format is not correct, error message box should be pop uped and when  I click ok, the text in the text box should be highlighted.

    For that I have given textBox1.SelectAll();

    But it is not getting highlighted.

     

    I would be very much obliged and thankful to you, if you could help me in this regard.

    Thanks and Regards,

    -jm

     

     

     

     

     

     

    Wednesday, January 16, 2008 1:51 PM