Closing a form
-
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(); } :)
The reason why the application exits automatically is that the entry point for it is located in a class usually called Program.cs that has the following code: static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } The last line (Application.Run(new Form1())), starts a message loop that ends as soon as the main form is closed (new Form1()), what causes the end of the application. To prevent the application from exiting I've found two possible solutions based on your original idea: 1. The first one is not to close the main form but to hide it: private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); this.Hide(); } 2. The second is to invoke the Application.Run() overload that doesn't take a Form as parameter but an ApplicationContext. This object (that always exist), implements an overridable method called OnMainFormClosed() that is responsible of shutting down the application when the main form is closed. To prevent this from happening, you can derive a new class from it that substitutes the original method by another that does nothing: //New class. class ApplicationContextOwn : ApplicationContext { //Overriden method does nothing protected override void OnMainFormClosed(object sender, EventArgs e) { } } static class Program { public static ApplicationContext ac = new ApplicationContext(); /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //The application is started using an instance of the new class. ApplicationContext ac = new ApplicationContextNew(); ac.MainForm = new Form1(); Application.Run(ac); } } Hope this helps.
-
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(); } :)
The application is closing because you're calling the main form that was started by the application.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001