form closing
-
I wanted to close a previous form after calling the next form. How do i do that? I have many form in my application. When i exit the application using the following code all the forms are closed one by one. Instead i want to close the previous form after calling the next form in order to avoid the closing of all the forms after exit. Please help. DialogResult result = MessageBox.Show("Do you want to exit?", "Exit-Window", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); if (result == DialogResult.OK) Application.Exit(); I have tried with the following code but the application exits automatically. The code is written on the command button on Form1. private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); this.Close(); } :)
-
I wanted to close a previous form after calling the next form. How do i do that? I have many form in my application. When i exit the application using the following code all the forms are closed one by one. Instead i want to close the previous form after calling the next form in order to avoid the closing of all the forms after exit. Please help. DialogResult result = MessageBox.Show("Do you want to exit?", "Exit-Window", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); if (result == DialogResult.OK) Application.Exit(); I have tried with the following code but the application exits automatically. The code is written on the command button on Form1. private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); this.Close(); } :)
Application.Exit is made to terminate an application regardless of which form is active. And if you close the main form (the very first form that appears when your application is started) then the application will terminate. To perform what you wish to accomplish you have several methods available. One method is that instead of closing the main form hide it whn ever you invoke a child form and then redisplay it when the child form closes. Depending on how many forms you have in your application you may want to make some of the forms seperate executables to keep memory usage low. Lastly the method that I use is to modify the Main method of the program. By default the Main method will just instantiate Form1 and then let its message processing loop run until the form closes. When I need to do what you are trying to do I alter the main method so that it will open a form and let it run but after that form closes I check to see what it was closed. If it were closed because the user needed to go to a different form then I instantiate that new form and allow its message processing loop to run. OTherwise I allow the MAin method to exit (thus terminating the application).
Joel Ivory Johnson
Meet my dev team: RDA Architecture Evangelist Team Blog
My site: J2i.net
Twitter: J2iNet